This repository has been archived on 2024-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
local-llm-server/llm_server/routes/cache.py

39 lines
999 B
Python

from flask_caching import Cache
from redis import Redis
cache = Cache(config={'CACHE_TYPE': 'RedisCache', 'CACHE_REDIS_URL': 'redis://localhost:6379/0', 'CACHE_KEY_PREFIX': 'local-llm'})
# redis = Redis()
class RedisWrapper:
"""
A wrapper class to set prefixes to keys.
"""
def __init__(self, prefix, **kwargs):
self.redis = Redis(**kwargs)
self.prefix = prefix
def set(self, key, value):
return self.redis.set(f"{self.prefix}:{key}", value)
def get(self, key):
return self.redis.get(f"{self.prefix}:{key}")
def incr(self, key, amount=1):
return self.redis.incr(f"{self.prefix}:{key}", amount)
def decr(self, key, amount=1):
return self.redis.decr(f"{self.prefix}:{key}", amount)
def flush(self):
flushed = []
for key in self.redis.scan_iter(f'{self.prefix}:*'):
flushed.append(key)
self.redis.delete(key)
return flushed
redis = RedisWrapper('local_llm')