-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
84 lines (68 loc) · 2.21 KB
/
cache.py
File metadata and controls
84 lines (68 loc) · 2.21 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Caching layer:
Attempts to use Redis when `REDIS_URL` environment variable is set and the `redis`
package is installed. Falls back to a thread-safe in-memory cache when Redis is
unavailable. The interface mirrors the previous SimpleCache: `set`, `get`, `clear`.
"""
import os
import threading
import time
try:
import redis
except Exception:
redis = None
class SimpleCache:
def __init__(self):
self._store = {}
self._lock = threading.Lock()
def set(self, key, value, ttl=300):
expire = time.time() + ttl
with self._lock:
self._store[key] = (value, expire)
def get(self, key):
with self._lock:
v = self._store.get(key)
if not v:
return None
value, expire = v
if expire < time.time():
# expired
del self._store[key]
return None
return value
def clear(self):
with self._lock:
self._store.clear()
class RedisCache:
def __init__(self, url=None, prefix='es:'):
if not redis:
raise RuntimeError('redis package is not available')
self.prefix = prefix
self.client = redis.from_url(url or os.environ.get('REDIS_URL'), decode_responses=True)
# Ensure connection works (raises if not)
self.client.ping()
def _key(self, key):
return f"{self.prefix}{key}"
def set(self, key, value, ttl=300):
self.client.set(self._key(key), value, ex=ttl)
def get(self, key):
return self.client.get(self._key(key))
def clear(self):
# Remove keys matching prefix
pattern = f"{self.prefix}*"
for k in self.client.scan_iter(match=pattern):
self.client.delete(k)
# Choose backend: RedisCache if REDIS_URL is set and redis is available, else SimpleCache
_cache = None
try:
redis_url = os.environ.get('REDIS_URL')
if redis and redis_url:
try:
_cache = RedisCache(redis_url)
except Exception:
# Fall back to in-memory cache if Redis is unreachable
_cache = SimpleCache()
else:
_cache = SimpleCache()
except Exception:
_cache = SimpleCache()
cache = _cache