-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.py
50 lines (40 loc) · 1.25 KB
/
cache.py
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
"""
Cache module
"""
import time
class Cache(object):
""" Class that caches the Vercel API responses """
_cache_ = {}
VALUE = 0
EXPIRES = 1
def __init(self):
pass
@classmethod
def get(cls, key):
"""Get the value from the cache stored with 'key' if it exists"""
try:
if cls._cache_[key][cls.EXPIRES] > time.time():
return cls._cache_[key][cls.VALUE]
del cls._cache_[key] # Delete the item if it has expired
return None
except KeyError:
return None
@classmethod
def set(cls, key, value, duration=3600):
"""Store/overwite a value in the cache with 'key' and an optional duration (seconds)"""
try:
expires = time.time() + duration
except TypeError:
raise TypeError("Duration must be numeric")
cls._cache_[key] = (value, expires)
return cls.get(key)
@classmethod
def clean(cls):
"""Remove all expired items from the cache"""
for key, _ in cls._cache_.items():
# Attempting to fetch an expired item deletes it
cls.get(key)
@classmethod
def purge(cls):
"""Remove all items from the cache"""
cls._cache_ = {}