-
Notifications
You must be signed in to change notification settings - Fork 3
/
handle_redis.py
193 lines (172 loc) · 6.06 KB
/
handle_redis.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
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python
"""
This file acts as a platform on which all your Redis actions are
performed. All Necessary seetings and configurations are build and
handled in this file.
An object of this can be created and used as per the user convenience.
"""
import redis
import json
class RedisHandler(object):
"""
This is the class that user needs to make use of, for handling
all REDIS related activities.
This provides a simple interface which the users can make use
of and handle the Redis Changes. The contents of this file,
for this demo application is very primitive and can be extended.
"""
def __init__(self, host="localhost", port=6379, password=""):
"""
Constructor Function.
@:parameter
host - Redis Host
port - Redis Connection Port
password - Redis Connection Password
"""
self.host = host
self.port = port
self.password = password
self.redis = None
self.error_found = False
self.__initialize()
self.info = None
self.error = ""
def __initialize(self):
"""
This is the base initializer function that is invoked from
the __init__ function. This sets-up all your REDIS required
constraints.
"""
try:
self.redis = redis.StrictRedis(
host=self.host,
port=self.port,
password=self.password)
self.info = self.redis.info()
except redis.ConnectionError:
"""
For the purpose of this function, lets set a variable saying
we faced an error setting up REDIS.
"""
self.error = "Connection Error trying to Access REDIS."
self.error_found = True
def add_to_redis(self, key, value=None):
"""
This function acts as an interface that the user can make use of
for creating an item in the REDIS Cache for Processing/any other
req.
@:parameter
key - Key to use for Setting Redis Item.
value - Value to be set for the key.
"""
try:
self.redis.set(key, value)
except:
self.error_found = True
self.error = "Failed to Set Value " + value + " to Key " + key
def get_from_redis(self, key, auth=False):
"""
This function acts as an interface while the user tries to obtain
a value from the REDIS cache.
@:parameter
key - Key to Check in Redis
@:return
value - Value for the Key mentioned above that's in REDIS.
"""
try:
if key is None:
self.error_found = True
self.error = "Can't have a None/Null value for Key while " + \
"trying read data from REDIS"
if auth:
return False
else:
return ""
else:
return self.redis.get(key)
except:
self.error_found = True
self.error = "Failed to get value for Key " + key
if auth:
return False
else:
return self.error
def add_to_redis_list(self, key, value=None):
"""
This function acts as an interface to let the user add an item to
an array in REDIS.
@:parameter
key - Key to use for Setting Redis Item.
value - Value to Append to the Redis List.
"""
try:
self.redis.rpush(key, value)
except:
self.error_found = True
self.error = "Failed to Append Value " + value + \
" to list with Key " + key
def check_error(self):
"""
This function acts as an error check mechanism to safely handle
all the breakage scenario.
@:return
error - Error Message
"""
if self.error_found:
return self.error
def get_all_keys(self):
"""
This function acts as an interface that can be used to obtain a
list of all the Key value pairs in the Redis Cache.
This returns a list of Lists with 2 values in each of the internal
lists.
@:return
redis_data - List of Redis Items stored.
"""
redis_data = list()
try:
redis_keys = self.redis.keys()
for key in redis_keys:
value = self.redis.get(key)
if type(value) is list:
temp = ""
for v in value:
temp += str(v)
value = temp
item = dict()
item['key'] = key
item['value'] = value
redis_data.append(item)
return redis_data
except:
self.error_found = True
self.error = "Failed to Obtain Redis Key value Info."
return ["Error", self.error]
def reset_error(self):
"""
Reset Error Message.
"""
self.error_found = False
self.error = ""
def get_redis_info(self):
"""
This acts as a wrapper to be used for obtaining the Redis
connection related information.
@:return
info - A Dictionary containing Redis Info.
"""
try:
return json.dumps(
self.redis.info(),
sort_keys=True,
indent=8,
separators=(',', ': '))
except:
self.error_found = True
self.error = "Error Trying to Read Redis Infomration"
return self.error
def get_connection(self):
"""
Used for getting the Redis Connection Object.
"""
return self.redis