-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_provider.py
60 lines (46 loc) · 1.52 KB
/
redis_provider.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
51
52
53
54
55
56
57
58
59
60
import time
import redis as redis
from config import Config
from models.cell import Cell
class RedisProvider:
get_seconds = 0
calc_seconds = 0
client = redis.StrictRedis(host=Config.redis_host, port=Config.redis_port, db=Config.redis_db)
key = Config.file_name + ":"
@staticmethod
def set_cell(cell_id, value):
RedisProvider.client.set(RedisProvider.key + cell_id, RedisProvider.to_redis_cells(value))
return
@staticmethod
def set(id, value):
RedisProvider.client.set(RedisProvider.key + id, value)
return
@staticmethod
def get(id):
value = RedisProvider.client.get(RedisProvider.key + id)
value = value.decode("utf-8")
return value
@staticmethod
def get_cell(cell_id):
#t0 = time.time()
ids = RedisProvider.client.get(RedisProvider.key + cell_id)
#RedisProvider.get_seconds = RedisProvider.get_seconds + time.time() - t0
if ids is None:
return None
#t0 = time.time()
cells = RedisProvider.from_redis_cells(ids)
#RedisProvider.calc_seconds = RedisProvider.calc_seconds + time.time() - t0
return cells
@staticmethod
def to_redis_cells(cell_ids):
return ';'.join(cell_ids)
@staticmethod
def from_redis_cells(ids):
ids = ids.decode("utf-8")
cell_ids = ids.split(";")
cells = []
if len(ids) < 3:
return cells
for cell_id in cell_ids:
cells.append(cell_id)
return cells