redis数据库list类型各方法封装成类
时间:2021-07-01 10:21:17
帮助过:23人阅读
import redis
2
3
4 class MyRedisList(object):
5 def __init__(self, key):
6 self.key =
key
7 self.connection = redis.StrictRedis(db=0, decode_responses=
True)
8
9 def add(self, direction=
"r", *values):
# 增加数据(lpush rpush)
10 if direction ==
"r":
11 self.connection.rpush(self.key, *
values)
12 elif direction ==
"l":
13 self.connection.lpush(self.key, *
values)
14
15 def pop(self, direction=
"r"):
# 删除数据(首尾)
16 if direction ==
"r":
17 self.connection.rpop(self.key)
18 elif direction ==
"l":
19 self.connection.lpop(self.key)
20
21 def rem(self, count, value):
# 指定位置删除
22 self.connection.lrem(self.key, count, value)
23
24 def get(self, index1, index2=False):
# 获取数据
25 if index2:
26 return self.connection.lrange(self.key, index1, index2)
27 elif index2
is False:
28 return self.connection.lindex(self.key, index1)
29
30 def update(self, index, values):
# 指定位置修改
31 self.connection.lset(self.key, index, values)
32
33
34 if __name__ ==
‘__main__‘:
35 re = MyRedisList(
"my_list")
36 re.add(
"r", 1, 2, 3
)
37 re.rem(0, 3
)
38 re.update(2,
"gkl")
39 print(re.get(0, -1
))
40 print(re.get(4))
redis数据库list类型各方法封装成类
标签:dir style push 定位 red pop sel 指定 list