-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
114 lines (98 loc) · 2.74 KB
/
helpers.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
import redis
from redis.connection import Encoder
class RedisHelper:
"""
RedisHelper
~~~~~~~~~~~
A project based redis helper class
"""
def __init__(self, host, port, db, **kwargs):
self.instance = redis.Redis(
host=host,
port=port,
db=db,
charset="utf-8",
decode_responses=True,
**kwargs
)
@staticmethod
def to_byte(value):
"""
Convert value to byte
:param value:
:return:
"""
accepted_types = (bytes, str, int, float)
assert isinstance(value, accepted_types), (
"Given value type cannot be {}, should be one of them {}".format(type(value), accepted_types)
)
encoder = Encoder(
encoding='utf-8',
encoding_errors='strict',
decode_responses=True
)
return encoder.encode(value)
def get_value(self, key):
"""
Get single value given key
:param key:
:return:
"""
key = self.to_byte(key)
return self.instance.get(key)
def get_all_keys(self, search_key_expression):
"""
Get all keys with expression
:param search_key_expression:
:return:
"""
return self.instance.keys(pattern=search_key_expression)
def set_value(self, key, value, **kwargs):
"""
Set one value to key, create new one or update exist one
:param key:
:param value:
:return:
"""
key = self.to_byte(key)
return self.instance.set(key, value, **kwargs)
def set_value_expire_time(self, key, value, expire_time):
"""
Set key with expire time
:param key:
:param value:
:param expire_time:
:return:
"""
return self.set_value(key, value, ex=expire_time)
def is_key_exists(self, key):
"""
Check if key exist
:param key:
:return:
"""
key = self.to_byte(key)
return self.instance.exists(key)
def delete_key(self, key):
"""
Delete one key
:param key:
:return:
"""
key = self.to_byte(key)
return self.instance.delete(key)
def delete_all_keys(self):
"""
Delete all key, in redis it means flush db
:return:
"""
is_deleted = self.instance.flushdb()
return is_deleted
def get_hash_value(self, hash):
hash = self.to_byte(hash)
return self.instance.hgetall(hash)
def insert_hash(self, hash, key, value): #Hash
key = self.to_byte(key)
value = self.to_byte(value)
hash = self.to_byte(hash)
self.instance.hset(hash, key, value)