fallback cache
This commit is contained in:
parent
47347843d0
commit
8675edf19f
1 changed files with 41 additions and 1 deletions
42
ox/cache.py
42
ox/cache.py
|
@ -456,7 +456,47 @@ class RedisCache(KVCache):
|
|||
self.backend = redis.from_url(self.url)
|
||||
|
||||
|
||||
if cache_path().startswith('fs:'):
|
||||
class FallbackCache(KVCache):
|
||||
caches = []
|
||||
|
||||
def __init__(self):
|
||||
fallback = cache_path()
|
||||
for path in fallback.split('|'):
|
||||
os.environ['oxCACHE'] = path
|
||||
if path.startswith('redis:'):
|
||||
store = RedisCache()
|
||||
elif path.startswith('memcache:'):
|
||||
store = MemCache()
|
||||
self.caches.append(store)
|
||||
os.environ['oxCACHE'] = fallback
|
||||
|
||||
def get(self, url, data, headers=None, timeout=-1, value="data"):
|
||||
if timeout == 0:
|
||||
return None
|
||||
|
||||
info_key, data_key = self._keys(url, data, headers)
|
||||
for cache in self.caches:
|
||||
try:
|
||||
info = cache.backend.get(info_key)
|
||||
except:
|
||||
info = None
|
||||
if info:
|
||||
return cache.get(url, data, headers, timeout, value)
|
||||
return None
|
||||
|
||||
def set(self, url, post_data, data, headers):
|
||||
self.caches[0].set(url, post_data, data, headers)
|
||||
for cache in self.caches[1:]:
|
||||
cache.delete(url, post_data, headers)
|
||||
|
||||
def delete(self, url, data=None, headers=None):
|
||||
for cache in self.caches:
|
||||
cache.delete(url, data, headers)
|
||||
|
||||
|
||||
if '|' in cache_path():
|
||||
store = FallbackCache()
|
||||
elif cache_path().startswith('fs:'):
|
||||
store = FileCache()
|
||||
elif cache_path().startswith('redis:'):
|
||||
store = RedisCache()
|
||||
|
|
Loading…
Reference in a new issue