forked from ref-finance/indexer-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_provider.py
99 lines (76 loc) · 2.74 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from config import Cfg
import redis
pool = redis.ConnectionPool(host='127.0.0.1',port=6379,decode_responses=True)
def list_farms(network_id):
import json
r=redis.StrictRedis(connection_pool=pool)
ret = r.hgetall(Cfg.NETWORK[network_id]["REDIS_KEY"])
r.close()
list_farms = [json.loads(x) for x in ret.values()]
print(len(list_farms))
return list_farms
def list_pools(network_id):
import json
r=redis.StrictRedis(connection_pool=pool)
ret = r.hgetall(Cfg.NETWORK[network_id]["REDIS_POOL_KEY"])
r.close()
pools = {}
for id, value in ret.items():
pools[id] = json.loads(value)
return pools
def list_top_pools(network_id):
import json
r=redis.StrictRedis(connection_pool=pool)
ret = r.hgetall(Cfg.NETWORK[network_id]["REDIS_TOP_POOL_KEY"])
r.close()
pools = {}
for key, value in ret.items():
pools[key] = json.loads(value)
return pools
class RedisProvider(object):
def __init__(self):
self.r=redis.StrictRedis(connection_pool=pool)
self.pipe = None
def begin_pipe(self) -> bool:
if self.pipe is None:
self.pipe = self.r.pipeline(transaction=True)
self.farmids = set()
return True
else:
return False
def end_pipe(self) -> bool:
if self.pipe is not None:
self.pipe.execute()
self.pipe = None
return True
else:
return False
# remove farms that not existed in contract
def review_farmid(self, network_id):
old = set(self.r.hkeys(Cfg.NETWORK[network_id]["REDIS_KEY"]))
expired = list(old - self.farmids)
self.r.hdel(Cfg.NETWORK[network_id]["REDIS_KEY"], *expired)
return expired
def add_farm(self, network_id, farm_id, farm_str):
self.farmids.add(farm_id)
self.r.hset(Cfg.NETWORK[network_id]["REDIS_KEY"], farm_id, farm_str)
def add_pool(self, network_id, pool_id, pool_str):
self.r.hset(Cfg.NETWORK[network_id]["REDIS_POOL_KEY"], pool_id, pool_str)
def add_top_pool(self, network_id, pool_id, pool_str):
self.r.hset(Cfg.NETWORK[network_id]["REDIS_TOP_POOL_KEY"], pool_id, pool_str)
def list_farms(self, network_id):
return self.r.hgetall(Cfg.NETWORK[network_id]["REDIS_KEY"])
if __name__ == '__main__':
conn = RedisProvider()
# conn.begin_pipe()
# conn.add_farm("farm_id_1", "farm_value_1")
# conn.add_farm("farm_id_2", "farm_value_2")
# conn.add_farm("farm_id_3", "farm_value_3")
# conn.end_pipe()
p = list_farms("TESTNET")
print(p)
list_pools("MAINNET")
# r=redis.StrictRedis(connection_pool=pool)
# r.hset(KEY, "aaa", "AAA")
# r.hset(KEY, "bbb", "BBB")
# r.close()