31 lines
962 B
Python
31 lines
962 B
Python
from functools import wraps
|
|
|
|
from . import RedisCache
|
|
|
|
|
|
def fetch_from_cache(cache_name: str, cache_config: dict):
|
|
cache_conn = RedisCache(cache_config["redis"])
|
|
ttl = cache_config["ttl"]
|
|
|
|
"""
|
|
It checks if a cached value exists for a given cache_name.
|
|
If a cached value exists, it returns the cached value.
|
|
If no cached value exists, it calls the original function f with the provided arguments, caches the result, and then returns the result.
|
|
"""
|
|
|
|
def decorator(f):
|
|
@wraps(f)
|
|
def wrapper(*args, **kwargs):
|
|
cached_value = cache_conn.get_value(cache_name)
|
|
# print("cached_value=", cached_value)
|
|
if cached_value:
|
|
return cached_value
|
|
response = f(*args, **kwargs)
|
|
# print("response=", response)
|
|
cache_conn.set_value(cache_name, response, ttl)
|
|
return response
|
|
|
|
return wrapper
|
|
|
|
return decorator
|