39 lines
999 B
Python
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')
|