Spaces:
Sleeping
Sleeping
File size: 1,838 Bytes
7644eac |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
"""
Simple script to clear the Redis cache.
Run this when you need to reset all cached learning paths.
"""
import redis
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
REDIS_HOST = os.getenv('REDIS_HOST', 'localhost')
REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
REDIS_PASSWORD = os.getenv('REDIS_PASSWORD', '').strip() # Strip whitespace
REDIS_DB = int(os.getenv('REDIS_DB', 0))
print(f"π Connecting to Redis at {REDIS_HOST}:{REDIS_PORT} (password: {'set' if REDIS_PASSWORD else 'none'})")
try:
# Build Redis connection params
redis_params = {
'host': REDIS_HOST,
'port': REDIS_PORT,
'db': REDIS_DB,
'decode_responses': True
}
# Only add password if it's not empty
if REDIS_PASSWORD:
redis_params['password'] = REDIS_PASSWORD
print("π Using password authentication")
redis_client = redis.Redis(**redis_params)
# Get all cache keys
path_keys = list(redis_client.scan_iter(match="path_cache:*"))
semantic_keys = list(redis_client.scan_iter(match="semantic_cache:*"))
total_keys = len(path_keys) + len(semantic_keys)
if total_keys == 0:
print("β
Cache is already empty!")
else:
# Delete all cache keys
if path_keys:
redis_client.delete(*path_keys)
print(f"ποΈ Deleted {len(path_keys)} learning path cache entries")
if semantic_keys:
redis_client.delete(*semantic_keys)
print(f"ποΈ Deleted {len(semantic_keys)} semantic cache entries")
print(f"β
Successfully cleared {total_keys} total cache entries!")
except Exception as e:
print(f"β Error clearing cache: {e}")
print("Make sure Redis is running and your .env file is configured correctly.")
|