File size: 788 Bytes
3a3a58f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
"""In-memory TTL cache. Replace with Redis when needed."""
import time
import hashlib
import json
from functools import wraps

_CACHE = {}


def cached(ttl: int = 3600):
    """Decorator: cache function result for `ttl` seconds."""
    def decorator(fn):
        @wraps(fn)
        def wrapper(*args, **kwargs):
            key = f"{fn.__name__}:{hashlib.md5(json.dumps([args, kwargs], default=str).encode()).hexdigest()}"
            now = time.time()
            if key in _CACHE:
                val, expires = _CACHE[key]
                if now < expires:
                    return val
            result = fn(*args, **kwargs)
            _CACHE[key] = (result, now + ttl)
            return result
        return wrapper
    return decorator


def cache_clear():
    _CACHE.clear()