team-3/src/c3nav/mapdata/utils/cache/stats.py

28 lines
690 B
Python
Raw Normal View History

2018-12-23 20:00:01 +01:00
from django.core.cache import cache
2018-12-25 21:34:48 +01:00
from django.utils import timezone
2018-12-23 20:00:01 +01:00
def increment_cache_key(cache_key):
try:
cache.incr(cache_key)
except ValueError:
cache.set(cache_key, 1, None)
2018-12-25 21:34:48 +01:00
def stats_snapshot(reset=True):
last_now = cache.get('apistats_last_reset', '', None)
now = timezone.now()
results = {}
for key in cache.keys('apistats__*'):
results[key] = cache.get(key)
if reset:
cache.delete(key)
if reset:
cache.set('apistats_last_reset', now, None)
results = dict(sorted(results.items()))
return {
'start_time': str(last_now),
'end_time': str(now),
'data': results
}