-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathcache.py
More file actions
38 lines (30 loc) · 942 Bytes
/
cache.py
File metadata and controls
38 lines (30 loc) · 942 Bytes
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
# Author: MoeClub.org, sxyazi
import pickle
import hashlib
try:
import redis
r = redis.Redis(host='127.0.0.1', port=6379, db=0)
# r = redis.Redis(host='Redis_URL', port=PORT, db=0, password='Password', ssl=True, ssl_ca_certs='ca.pem')
_ = r.client_list()
except:
import FakeRedis
r = FakeRedis.Cache()
class Cache:
CACHED_SECONDS = 768
@classmethod
def get(cls, path):
if cls.has(path):
return pickle.loads(r.get(cls._get_key(path)))
return False
@classmethod
def has(cls, path):
return r.exists(cls._get_key(path))
@classmethod
def set(cls, path, entity, expire=CACHED_SECONDS):
return r.set(cls._get_key(path), pickle.dumps(entity), expire)
@classmethod
def rem(cls, path):
return r.delete(cls._get_key(path))
@staticmethod
def _get_key(path):
return 'onelist:' + hashlib.md5(path.encode()).hexdigest()