拓展 Python 的字典,使其自动同步 Redis

最近在做点小东西的时候,用到了 redis 当数据库。基本上不怎么考虑效率,就想让代码看起来干净。

于是想怎么封装 python 的字典,让其进行增删改操作的时候,能自动存入 redis。

这代码只是个 ToyCode,目的只是想理清下 dict 的各 build-in 方法。大量使用这个类效率会非常低的

Talk is cheep, show you the code :)

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
try:
import cPickle as pickle
except:
import pickle
import redis, config

r = redis.Redis(host = config.REDIS_IP,\
port = config.REDIS_PORT,\
password = config.REDIS_PASSWORD,\
db = config.REDIS_DB)

class RedisDict(dict):
def __init__(self, key):
dict.__init__(self)
self.k = key
assert(self.k)
if r.exists(self.k):
self.__read__()
else:
self.__write__()

def __setitem__(self, key, val):
self.__read__()
dict.__setitem__(self, key, val)
self.__write__()

def __delitem__(self, key):
dict.__delitem__(self, key)
self.__write__()

def __getitem__(self, key):
self.__read__()
return dict.__getitem__(self, key)

def __write__(self):
r.set(self.k, pickle.dumps(self))

def __read__(self):
ret = pickle.loads(r.get(self.k))
if not dict.__eq__(self, ret):
dict.clear(self)
dict.update(self, ret)
self.__write__()

def clear(self):
dict.clear(self)
self.__write__()

def __eq__(self, other):
self.__read__()
if type(other) is type(self):
other.__read__()
return dict.__eq__(self, other)

def __cmp__(self, other):
self.__read__()
if type(other) is type(self):
other.__read__()
return dict.__cmp__(self, other)

def __repr__(self):
self.__read__()
return dict.__repr__(self)

def __le__(self, other):
self.__read__()
if type(other) is type(self):
other.__read__()
return dict.__le__(self, other)

def __ne__(self, other):
self.__read__()
if type(other) is type(self):
other.__read__()
return dict.__ne__(self, other)

def __lt__(self, other):
self.__read__()
if type(other) is type(self):
other.__read__()
return dict.__lt__(self, other)

def update(self, other):
self.__read__()
if type(other) is type(self):
other.__read__()
dict.update(self, other)
self.__write__()


def test():
dict1 = RedisDict("R")
dict2 = RedisDict("R")

dict1.clear()

assert(dict2 == dict1 == {})

dict1['key1'] = "hi"
assert(dict1['key1'] == dict2['key1'] == "hi")
assert(dict1 == dict2)

dict2.clear()
assert(dict1 == dict2 == {})

dict3 = RedisDict("R")
assert(dict3 == {})

dict3.update({'awesome': 'python'})
assert(dict1 == dict2 == dict3 == {'awesome': 'python'})

dict4 = RedisDict('K')
dict4['t'] = "other"
dict3.update(dict4)
assert(dict3 == {'awesome': 'python', 't': 'other'})

dict4.clear()
assert(dict3 == {'awesome': 'python', 't': 'other'})

if __name__ == "__main__":
test()

拓展 Python 的字典,使其自动同步 Redis

https://robinxb.com/posts/2014/python-dict-with-redis/

作者

薯条

发布于

2014-11-16

更新于

2024-01-15

许可协议

评论