fix: always enable cache

pull/14/head
Raphaël Vinot 2018-12-17 19:50:55 +01:00
parent f3ebe74042
commit 5df3add8de
1 changed files with 30 additions and 38 deletions

View File

@ -39,35 +39,32 @@ try:
except Exception: except Exception:
sphinx = False sphinx = False
enable_cache = True
r_cache = None r_cache = None
def _cache_init(): def _cache_init():
global r_cache global r_cache
if enable_cache and r_cache is None: if r_cache is None:
r_cache = redis.Redis(unix_socket_path=get_socket_path('cache'), db=1, decode_responses=True) r_cache = redis.Redis(unix_socket_path=get_socket_path('cache'), db=1, decode_responses=True)
def _cache_set(key, value, field=None): def _cache_set(key, value, field=None):
_cache_init() _cache_init()
if enable_cache: if field is None:
if field is None: r_cache.setex(key, json.dumps(value), 3600)
r_cache.setex(key, json.dumps(value), 3600) else:
else: r_cache.hset(key, field, json.dumps(value))
r_cache.hset(key, field, json.dumps(value)) r_cache.expire(key, 3600)
r_cache.expire(key, 3600)
def _cache_get(key, field=None): def _cache_get(key, field=None):
_cache_init() _cache_init()
if enable_cache: if field is None:
if field is None: value_json = r_cache.get(key)
value_json = r_cache.get(key) else:
else: value_json = r_cache.hget(key, field)
value_json = r_cache.hget(key, field) if value_json is not None:
if value_json is not None: return json.loads(value_json)
return json.loads(value_json)
return None return None
@ -80,35 +77,32 @@ def to_bool(s):
def get_submissions(url, day=None): def get_submissions(url, day=None):
_cache_init() _cache_init()
if enable_cache: if day is None:
if day is None: day = date.today().isoformat()
day = date.today().isoformat() else:
else: day = day.isoformat()
day = day.isoformat() key = date.today().isoformat() + '_submissions'
key = date.today().isoformat() + '_submissions' return r_cache.zscore(key, url)
return r_cache.zscore(key, url)
def get_mail_sent(url, day=None): def get_mail_sent(url, day=None):
_cache_init() _cache_init()
if enable_cache: if day is None:
if day is None: day = date.today().isoformat()
day = date.today().isoformat() else:
else: day = day.isoformat()
day = day.isoformat() key = date.today().isoformat() + '_mails'
key = date.today().isoformat() + '_mails' return r_cache.sismember(key, url)
return r_cache.sismember(key, url)
def set_mail_sent(url, day=None): def set_mail_sent(url, day=None):
_cache_init() _cache_init()
if enable_cache: if day is None:
if day is None: day = date.today().isoformat()
day = date.today().isoformat() else:
else: day = day.isoformat()
day = day.isoformat() key = date.today().isoformat() + '_mails'
key = date.today().isoformat() + '_mails' return r_cache.sadd(key, url)
return r_cache.sadd(key, url)
def is_valid_url(url): def is_valid_url(url):
@ -545,8 +539,6 @@ def get_url_data(url):
def cached(url): def cached(url):
_cache_init() _cache_init()
if not enable_cache:
return [url]
url_data = get_url_data(url) url_data = get_url_data(url)
to_return = [url_data] to_return = [url_data]
if url_data[url].get('list') is not None: if url_data[url].get('list') is not None: