python操作redis数据
一、环境安装
1、redispy安装
(automatic) C:\Users\Administrator>pip install redis
2、检测是否安装成功
(automatic) C:\Users\Administrator>python Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import redis >>>
二、python操作redis
1、创建基类
主要用于连接数据库、数据库单例:
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='192.168.0.104',port=6379,db=0,password='xxx') self.r=redis.Redis(connection_pool=pool)
2、String操作
class RedisString(RedisBase): def set(self): """设置值""" result=self.r.set('widh',58) return result def get(self): """获取值""" result=self.r.get('width') return result def mset(self): """设置多个键值对""" result=self.r.mset( { 'salary1':5000, 'salary2':6000 } ) return result def mget(self): """获取多个值""" result=self.r.mget( [ 'salary1', 'salary2' ] ) return result def delete(self): """删除键值""" result=self.r.delete('salary1') return result def incr(self): result=self.r.incr('salary1',20) return result
2、List操作
class RedisList(RedisBase): def lpush(self): result=self.r.lpush( 'l1',*[1,2,3,4,5] ) return result def lpop(self): """移除最左边的数据并返回""" result=self.r.lpop('l1') return result def lrange(self): """获取指定长度的数据""" result=self.r.lrange('l1',0,-1) return result
3、Set操作
class RedisSet(RedisBase): def sadd(self): """添加集合,返回的是成员的个数""" result=self.r.sadd('set1',*[1,2,3,4,5]) return result def sismember(self): """判断一个成员是否是集合的成员,返回布尔值""" result=self.r.sismember('set1',2) return result def smembers(self): """返回集合的所有成员""" result=self.r.smembers('set1') return result def sdiff(self): """返回与其它集合的差异元素""" result=self.r.sdiff('s5','s7') return result def sinder(self): """返回几个集合的交集""" result=self.r.sinter('s5','s7') return result def sunion(self): """返回几个集合的并集""" result=self.r.sunion('s5','s7') return result
4、完整实例
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='192.168.0.104',port=6379,db=0,password='bright123') self.r=redis.Redis(connection_pool=pool) class RedisString(RedisBase): def set(self): """设置值""" result=self.r.set('widh',58) return result def get(self): """获取值""" result=self.r.get('width') return result def mset(self): """设置多个键值对""" result=self.r.mset( { 'salary1':5000, 'salary2':6000 } ) return result def mget(self): """获取多个值""" result=self.r.mget( [ 'salary1', 'salary2' ] ) return result def delete(self): """删除键值""" result=self.r.delete('salary1') return result def incr(self): result=self.r.incr('salary1',20) return result class RedisList(RedisBase): def lpush(self): result=self.r.lpush( 'l1',*[1,2,3,4,5] ) return result def lpop(self): """移除最左边的数据并返回""" result=self.r.lpop('l1') return result def lrange(self): """获取指定长度的数据""" result=self.r.lrange('l1',0,-1) return result class RedisSet(RedisBase): def sadd(self): """添加集合,返回的是成员的个数""" result=self.r.sadd('set1',*[1,2,3,4,5]) return result def sismember(self): """判断一个成员是否是集合的成员,返回布尔值""" result=self.r.sismember('set1',2) return result def smembers(self): """返回集合的所有成员""" result=self.r.smembers('set1') return result def sdiff(self): """返回与其它集合的差异元素""" result=self.r.sdiff('s5','s7') return result def sinder(self): """返回几个集合的交集""" result=self.r.sinter('s5','s7') return result def sunion(self): """返回几个集合的并集""" result=self.r.sunion('s5','s7') return result def main(): rs=RedisString() rs1=RedisString() rl=RedisList() rl1=RedisList() rs=RedisSet() rs1=RedisSet() print(rs) print(rs1) print(rl) print(rl1) print(rs) print(rs1) # rs.set() # rs.mset() # print( rs.mget()) # print(rs.delete()) # print(rs.incr()) # print(rl.lpush()) # print(rl.lpop()) # print(rl.lrange()) # print(rs.sadd()) # print(rs1.sismember()) # print(rs.smembers()) # print(rs.sdiff()) # print(rs.sinder()) print(rs.sunion()) if __name__ == '__main__': main()
更多参考:
https://redis-py.readthedocs.io/en/latest/#contents
https://github.com/WoLpH/redis-py
三、Pipelines
pipline 是Redis类的子类,它为在单个请求中向服务器缓冲多个命令提供支持。
redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。
import redis pool = redis.ConnectionPool(host='192.168.0.104',port=6379,db=0,password='xxx') r = redis.Redis(connection_pool=pool) pipe = r.pipeline()#默认事务开启,具备原子性 pipe.set('rr1', 'v1') pipe.set('rr2', 'v2') pipe.execute()
更多参考:https://github.com/WoLpH/redis-py
作者:iveBoy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。