File size: 649 Bytes
4efde5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import json
from typing import Any
from services.redis import get_client


class _cache:
    async def get(self, key: str):
        redis = await get_client()
        key = f"cache:{key}"
        result = await redis.get(key)
        if result:
            return json.loads(result)
        return None

    async def set(self, key: str, value: Any, ttl: int = 15 * 60):
        redis = await get_client()
        key = f"cache:{key}"
        await redis.set(key, json.dumps(value), ex=ttl)

    async def invalidate(self, key: str):
        redis = await get_client()
        key = f"cache:{key}"
        await redis.delete(key)


Cache = _cache()