2020 Python 3.8 操作 redis 实战
#!/usr/bin/python# -*- coding: UTF-8 -*-import redisclass RedisBase:def __new__(cls, *args, **kwargs):"""单例模式"""if not hasattr(cls, "instance"):cls.instance ...
·
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import redis
class RedisBase:
def __new__(cls, *args, **kwargs):
"""单例模式"""
if not hasattr(cls, "instance"):
cls.instance = super(RedisBase, cls).__new__(cls, *args, **kwargs)
return cls.instance
def __init__(self):
"""每一个数据库实例管理一个连接池"""
pool = redis.ConnectionPool(host='xxx.xxx.xxx.xxx',port=6379,db=0,password='',decode_responses=True)
self.r=redis.Redis(connection_pool=pool)
class RedisString(RedisBase):
def set(self,k,v):
"""设置值"""
result=self.r.set(k,v)
return result
def get(self,k):
"""获取值"""
result=self.r.get(k)
return result
def mset(self,dmap):
"""设置多个键值对"""
result=self.r.mset(dmap)
return result
def mget(self,listKey):
"""获取多个值"""
result=self.r.mget(listKey)
return result
def delete(self,k):
"""删除键值"""
result=self.r.delete(k)
return result
def incr(self,k,v):
"""原有的value上,累加"""
result=self.r.incr(k,v)
return result
class RedisList(RedisBase):
def lpush(self,k,list):
result=self.r.lpush(
k,*list
)
return result
def lpop(self,k):
"""移除最左边的数据并返回"""
result=self.r.lpop(k)
return result
def lrange(self,k):
"""获取指定长度的数据"""
result=self.r.lrange(k,0,-1)
return result
class RedisSet(RedisBase):
def sadd(self,k,list):
"""添加集合,返回的是成员的个数"""
result=self.r.sadd(k,*list)
return result
def sismember(self,k,listV):
"""判断一个成员是否是集合的成员,返回布尔值"""
result=self.r.sismember(k,listV)
return result
def sgetList(self,k):
"""返回集合的所有成员"""
result=self.r.smembers(k)
return result
def sdiff(self,k1,k2):
"""返回与其它集合的差异元素"""
result=self.r.sdiff(k1,k2)
return result
def sinder(self,k1,k2):
"""返回几个集合的交集"""
result=self.r.sinter(k1,k2)
return result
def sunion(self,k1,k2):
"""返回几个集合的并集"""
result=self.r.sunion(k1,k2)
return result
class RedisHash(RedisBase):
def hmset(self,name,dmap):
result = self.r.hmset(name,dmap)
return result
def mainRedisString():
rs=RedisString()
print(rs)
#1.设置redis字符串 key,value
print(rs.set("key","value"))
#2.取值
print(rs.get("key"))
#3.存redis 字典map值
damp = {"key1":"1","key2":"123441213"}
print(rs.mset(damp))
#4.取redis多个key
list = ["key1","key2"]
print(rs.mget(list))
#5.删除redis key
#print(rs.delete("key1"))
#6.累加
print(rs.incr("key1",1))
def mainRedisList():
sl = RedisList()
print(sl)
#1.累加添加集合
list = [1,2,3,4,5,6]
print(sl.lpush("list",list))
#2.取出key中的集合
#print(sl.lpop("list"))
#3.取出所有集合数据
print(sl.lrange("list"))
def mainRedisSet():
rs = RedisSet()
print(rs)
#1.添加set list
list = [123,122,123,111,233,455,6,11,213123]
#print(rs.sadd("set2",list))
#2.查看set 集合中值是否存在
#print(rs.sismember("set1",111))
#3.获取set 集合中所有成员
#print(rs.sgetList("set1"))
#4.
#print(rs.sdiff("set1","set2"))
#print(rs.sinder("set1","set2"))
#print(rs.sunion("set1","set2"))
def mainRedisHash():
sh = RedisHash()
print(sh)
#1.hash 存放map
dmap = {"key1":"1234","key2":"1231","key3":"123" }
#print(sh.hmset("hash123",dmap))
if __name__ == '__main__':
#mainRedisString()
#mainRedisList()
#mainRedisSet()
mainRedisHash()
更多推荐
已为社区贡献2条内容
所有评论(0)