-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
33 lines (22 loc) · 872 Bytes
/
cache.py
File metadata and controls
33 lines (22 loc) · 872 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
from redis import Redis
from settings import REDIS_HOST, REDIS_PORT, REDIS_DB_NUMBER
class RedisCache:
def __init__(self, redis_host=REDIS_HOST, redis_port=REDIS_PORT, db_number=REDIS_DB_NUMBER):
self.redis_client = Redis(host=redis_host, port=redis_port, db=db_number, decode_responses=True)
self.redis_client.set_response_callback('GET', int)
def write_to_cache(self, accessed_uri):
"""
:type accessed_uri: str
"""
self.redis_client.incrby(accessed_uri)
def cache_to_dict(self):
"""
:rtype: dict
"""
cache_data = {}
for key in self.redis_client.keys():
cache_data.update({key.replace('.', '\\'): self.redis_client.get(key)})
return cache_data
def flush_cache(self):
return self.redis_client.flushdb()
MAIN_CACHE = RedisCache()