srht.cache: add generalized caching system

This commit is contained in:
Drew DeVault 2020-06-29 09:33:03 -04:00
parent f35f208ccd
commit c19708ed87
1 changed files with 22 additions and 0 deletions

22
srht/cache.py Normal file
View File

@ -0,0 +1,22 @@
"""
srht.cache is a simple wrapper around Redis which turns connection errors into
cache misses.
"""
from srht.redis import redis
def get_cache(key):
try:
return redis.get(key)
except:
return None
def set_cache(key, expr, value):
try:
redis.setex(key, expr, value)
except:
pass
def expunge_cache(key):
"""Failing to expunge the cache may be a security issue, so this is not
wrapped in a try/except"""
redis.delete(key)