Update utils/cache_manager.py
Browse files- utils/cache_manager.py +92 -47
utils/cache_manager.py
CHANGED
|
@@ -1,47 +1,92 @@
|
|
| 1 |
-
from diskcache import Cache
|
| 2 |
-
from functools import wraps
|
| 3 |
-
import hashlib
|
| 4 |
-
import json
|
| 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 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diskcache import Cache
|
| 2 |
+
from functools import wraps
|
| 3 |
+
import hashlib
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
from config import settings
|
| 7 |
+
|
| 8 |
+
# Handle Spaces environment - use /tmp if available
|
| 9 |
+
cache_dir = settings.CACHE_DIR
|
| 10 |
+
if not os.path.exists(cache_dir):
|
| 11 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
# Initialize cache with error handling
|
| 14 |
+
try:
|
| 15 |
+
cache = Cache(cache_dir, size_limit=1000000000) # 1GB limit
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Cache initialization failed: {e}. Using memory cache fallback.")
|
| 18 |
+
# Fallback to a simple dictionary-based cache
|
| 19 |
+
cache = {}
|
| 20 |
+
|
| 21 |
+
def cache_llm_response(expire=86400):
|
| 22 |
+
"""Cache decorator for LLM responses with fallback"""
|
| 23 |
+
def decorator(func):
|
| 24 |
+
@wraps(func)
|
| 25 |
+
def wrapper(*args, **kwargs):
|
| 26 |
+
if not settings.CACHE_ENABLED:
|
| 27 |
+
return func(*args, **kwargs)
|
| 28 |
+
|
| 29 |
+
# Create cache key
|
| 30 |
+
key_data = {
|
| 31 |
+
"func": func.__name__,
|
| 32 |
+
"args": str(args),
|
| 33 |
+
"kwargs": str(kwargs)
|
| 34 |
+
}
|
| 35 |
+
key = hashlib.md5(json.dumps(key_data, sort_keys=True).encode()).hexdigest()
|
| 36 |
+
|
| 37 |
+
# Check cache (with fallback for dictionary cache)
|
| 38 |
+
if isinstance(cache, dict):
|
| 39 |
+
if key in cache:
|
| 40 |
+
return cache[key]
|
| 41 |
+
else:
|
| 42 |
+
if key in cache:
|
| 43 |
+
return cache[key]
|
| 44 |
+
|
| 45 |
+
# Call function and cache result
|
| 46 |
+
result = func(*args, **kwargs)
|
| 47 |
+
|
| 48 |
+
# Store in cache (with fallback)
|
| 49 |
+
if isinstance(cache, dict):
|
| 50 |
+
cache[key] = result
|
| 51 |
+
else:
|
| 52 |
+
try:
|
| 53 |
+
cache.set(key, result, expire=expire)
|
| 54 |
+
except Exception:
|
| 55 |
+
# Fallback to dictionary if disk cache fails
|
| 56 |
+
cache[key] = result
|
| 57 |
+
|
| 58 |
+
return result
|
| 59 |
+
return wrapper
|
| 60 |
+
return decorator
|
| 61 |
+
|
| 62 |
+
def clear_cache():
|
| 63 |
+
"""Clear all cached responses"""
|
| 64 |
+
try:
|
| 65 |
+
if isinstance(cache, dict):
|
| 66 |
+
cache.clear()
|
| 67 |
+
else:
|
| 68 |
+
cache.clear()
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Cache clearing failed: {e}")
|
| 71 |
+
|
| 72 |
+
def get_cache_stats():
|
| 73 |
+
"""Get cache statistics"""
|
| 74 |
+
try:
|
| 75 |
+
if isinstance(cache, dict):
|
| 76 |
+
return {
|
| 77 |
+
"size": sum(len(str(v)) for v in cache.values()),
|
| 78 |
+
"count": len(cache),
|
| 79 |
+
"enabled": settings.CACHE_ENABLED
|
| 80 |
+
}
|
| 81 |
+
else:
|
| 82 |
+
return {
|
| 83 |
+
"size": cache.volume(),
|
| 84 |
+
"count": len(cache),
|
| 85 |
+
"enabled": settings.CACHE_ENABLED
|
| 86 |
+
}
|
| 87 |
+
except Exception:
|
| 88 |
+
return {
|
| 89 |
+
"size": 0,
|
| 90 |
+
"count": 0,
|
| 91 |
+
"enabled": False
|
| 92 |
+
}
|