-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisfuncs.py
184 lines (150 loc) · 4.93 KB
/
redisfuncs.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
## import constants
from const import redis_host, redis_port,redis_nodes,pending,executing,success,failed,retrial1,retrial2,starting,lock_suffix,rate_limit_suffix,valid_suffix,redis_lock_ttl_ms,const_true,const_false
## import uuid
import uuid
## redis
import redis
from redlock import Redlock
## Asyncio
import asyncio
from asyncer import asyncify
## Async wrapper for all Redis functions
async def async_redis_wrapper(func, *args):
return await asyncify(func)(*args)
## List all redis keys
def list_redis_keys():
r = redis.Redis(host=redis_host, port=redis_port)
keys = r.keys()
r.close()
return keys
## Check Whether a Redis Key Exists
def is_redis_key(key:str):
r = redis.Redis(host=redis_host, port=redis_port)
retval = bool(r.exists(key))
r.close()
return retval
## Get a redis key
def get_redis_key(key:str):
r = redis.Redis(host=redis_host, port=redis_port)
retval = r.get(key)
r.close()
return retval
## Get multiple redis keys
def get_redis_keys(keys:list):
r = redis.Redis(host=redis_host, port=redis_port)
retval = []
for key in keys:
retval.append(r.get(key))
r.close()
return retval
## Set a redis key
def set_redis_key(key:str,value:str):
r = redis.Redis(host=redis_host, port=redis_port)
r.set(key,value)
r.close()
## Set multiple redis keys
def set_redis_keys(key_values:dict):
r = redis.Redis(host=redis_host, port=redis_port)
for key,value in key_values.items():
r.set(key,value)
r.close()
## Execute a redis command
def execute_redis_command(command:str):
r = redis.Redis(host=redis_host, port=redis_port)
retval = r.execute_command(command)
r.close()
return retval
## Increment a redis key
def incr_redis_key(key:str):
r = redis.Redis(host=redis_host, port=redis_port)
retval = r.incr(key)
r.close()
return retval
## Decrement a redis key
def decr_redis_key(key:str):
r = redis.Redis(host=redis_host, port=redis_port)
retval = r.decr(key)
r.close()
return retval
## Acquire a redis lock
def acquire_redis_lock(key:str,ttl):
# Create a distributed lock across Redis Nodes
redlock = Redlock(redis_nodes)
# Acquire the lock on a Key
lock = redlock.lock(key,ttl)
if lock:
print("Lock success !!")
# Return the lock object
return lock
else:
print("Lock Failed !!")
return None
## Release a redis lock
def release_redis_lock(lock:Redlock):
if lock:
# Distributed lock Object across Redis Nodes
redlock = Redlock(redis_nodes)
# Release the lock
redlock.unlock(lock)
## Delete a redis key
def delete_redis_key(key:str):
r = redis.Redis(host=redis_host, port=redis_port)
if r.exists(key):
r.delete(key)
msg = f"Key {key} deleted"
else:
msg = f"Key {key} not found"
r.close()
return msg
## Delete all redis keys
def delete_all_redis_keys():
r = redis.Redis(host=redis_host, port=redis_port)
r.flushall()
r.close()
## Get full hash set using the hash set name
def get_hash_set(hash_name:str):
r = redis.Redis(host=redis_host, port=redis_port)
retval = r.hgetall(hash_name)
r.close()
return retval
## For a hash set get the values of specific key
def get_hash_set_key(hash_name:str,key:str):
r = redis.Redis(host=redis_host, port=redis_port)
retval = r.hget(hash_name,key)
r.close()
return retval
## For a hash set get the values of the keys
def get_hash_set_keys(hash_name:str,keys:list):
r = redis.Redis(host=redis_host, port=redis_port)
retval = []
for key in keys:
retval.append(r.hget(hash_name,key))
r.close()
return retval
## Set a hash set key value pair
def set_hash_set(hash_name:str,key:str,value:str):
r = redis.Redis(host=redis_host, port=redis_port)
r.hset(hash_name,key,value)
r.close()
## Set multiple hash set key value pairs
def set_hash_set_keys(hash_name:str,key_values:dict):
r = redis.Redis(host=redis_host, port=redis_port)
r.hmset(hash_name,key_values)
r.close()
## Generate a unique tracking id
def generate_unique_tracking_id():
# Generate a unique id (this is the tracking id for the save and finalise operation)
unique_id = str(uuid.uuid4())
# Inifnite loop to ensure that tracking id is always unique
while True:
# Check if the tracking id is not in redis, then its a valid id, add it and continue
if not is_redis_key(f"{unique_id}{valid_suffix}"):
set_redis_key(f"{unique_id}{valid_suffix}",const_true)
break ## break the loop
else: # if tracking id exists then regenarate the tracking id and continue the loop
isused = get_redis_key(f"{unique_id}{valid_suffix}")
if isused:
unique_id = str(uuid.uuid4())
continue
break ## break the loop
return unique_id ## return the unique tracking id