-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathredis_helper.py
41 lines (26 loc) · 926 Bytes
/
redis_helper.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
# Redis helper
#
# Functions to help read and write from Redis
from config import Config_Redis
import redis
import json
class RedisConnect():
def __init__(self):
# Load global variables
self.redis_url = Config_Redis.redis_url
self.redis_port = Config_Redis.redis_port
self.redis_password = Config_Redis.redis_password
self.r = redis.Redis( # Connect to Redis
host=self.redis_url,
port=self.redis_port
)
def redis_read(self,key): # Read data from Redis
results = self.r.get(key) # Get the latest results from Redis for a given key
if results:
data = json.loads(results)
else:
data = ""
return data
def redis_write(self,key,data,ttl): # Write data to Redis
write = self.r.set(key,json.dumps(data),ttl) # Store data with a given TTL
return write