您的位置:首页 > 数据库 > Redis

使用python对redis操作

2016-04-10 16:59 477 查看
写在前面
首先声明,这是为了学习python对redis操作而写的一个小demo,包括了这几天网站找到的一些资料,综合总结出来一些东西,最后附上我写的一个用python操作redis的一个demo:

模块安装
python提供了一个模块redis-py来使我们很方便的操作redis数据库,安装该模块也很简单,直接使用pip安装就行,命令如下:
pip install redis
安装完之后,使用import调用一下就能知道是否安装成功,在python界面下输入import redis,如果不报错,那么该模块就算安装成功了。
模块安装成功后,就可以创建redis连接了,接下来学习怎样创建redis连接:
redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令(比如,SET命令对应与StrictRedis.set方法)。Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。 简单说,官方推荐使用StrictRedis方法。

这里不推荐使用Redis类,原因是他和咱们在redis-cli操作有些不一样,主要不一样是下面这三个方面。
(1)LREM:参数 ‘num’ 和 ‘value’ 的顺序交换了一下,cli是 lrem queueName 0 ‘string’ 。这里的0是所有的意思。 但是Redis这个类,把控制和string调换了。
(2)ZADD:实现时 score 和 value 的顺序不小心弄反了,后来有人用了,就这样了
(3)SETEX: time 和 value 的顺序反了
我们可以使用StrictRedis直接连接redis,代码如下:
#创建一个redis连接
r=redis.StrictRedis(host='192.168.7.25',port=6379,db=0)
连接池的使用
但是,对于大量redis连接来说,如果使用直接连接redis的方式的话,将会造成大量的TCP的重复连接,所以,推荐用连接池来解决这个问题,使用连接池连接上redis之后,可以从该连接池里面生成连接,调用完成之后,该链接将会返还给连接池,供其他连接请求调用,这样将减少大量redis连接的执行时间,下面介绍两个类Redis和StrictRedis的连接池的实现方式:

Redis的连接池的方法:
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
StrictRedis的连接池的实现方式:
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
r = redis.StrictRedis(connection_pool=pool)
官方提供的redis连接池可以添加的参数:
class redis.StrictRedis(host='localhost', port=6379, db=0, password=None, socket_timeout=None, connection_pool=None, charset='utf-8', errors='strict', decode_responses=False, unix_socket_path=None)
Implementation of the Redis protocol.This abstract class provides a Python interface to all Redis commands and an implementation of the Redis protocol.
Connection and Pipeline derive from this, implementing how the commands are sent and received to the Redis server
一些小demo
接下来,需要提出redis的几种数据类型了,我使用redis客户端连接redis的时候,发现redis支持五种数据类型的key,列表如下:
string:存储string,以及int或者float等数据类型的数据。
list:存储列表类型的数据
hash:存储字典类型的数据
set:存储集合类型的数据
zset:存储的也是集合类型的数据,不过数据是有序存储的(该类型目前demo里面没有加入。。。)
好了,了解了redis支持的数据类型之后,就可以写一些小demo了,下面是一些小demo:
#创建一个string类型的key并放入value
r.set("abc",34634)
#创建一个hash
r.hset('abc:def', 'name', "abcde")
#获取一个hash的所有值
print r.hgetall('abc:def')
#获取一个hash的所有key
print r.hkeys('abc:def')
#创建list
r.sadd('abcd:ef','nihao')
r.sadd('abcd:ef','hello')
r.sadd('xxxx','nihao')
r.sadd('xxxx','good')
#打印出该key中的值 list
print r.smembers('abcd:ef')
#查询两个list中相同的值
print r.sinter('abcd:ef', 'xxxx')
#给两个list取并集
print r.sunion('abcd:ef', 'xxxx')
#列出所有key
print r.keys()
#列出以abc开头的所有key
print r.keys("abc*")
demo总结
由于之前公司碰到过需求,需要批量修改一批key的value,之前使用shell也可以做,不过,现在就通过完成这个需求使用python来实现这个小demo吧。
该demo中将会附上我的一些解释,希望各路大神不要吐槽。。。很久没动手写python了,再写就是各种蛋疼。。。。。。
#!/usr/bin/env python
#coding=utf-8
import redis    #导入redis-py模块
class RedisPool:    #定义了一个连接池类,该类返回连接池中的一个连接给调用者
def Redis_Pool(self,ClientHost="192.168.0.25",ClientPort=6379,ClientDb=0):
pool=redis.ConnectionPool(host=ClientHost,port=ClientPort,db=ClientDb)
return redis.StrictRedis(connection_pool=pool)
class ChangeKey: #该类使用获取到的redis连接对想要进行修改的key进行修改

def Change_String(self,R,Key,Value):
try:
Bool = R.set(Key,Value)
except Exception as e:
Bool = False
print 'Insert string Error:',e
return Bool

def Change_List(self,R,KeyName,Key):
Filed_List=[]
for i in range(0,len(Key)):
try:
R.rpush(KeyName,Key[i])
except Exception as e:
Filed_List.append((KeyName,Key[i]))
print 'Insert set Error:',e
return Filed_List

def Change_Hash(self,R,KeyName,Key):
Filed_List=[]
for i in Key.keys():
try:
R.hset(KeyName,i,Key[i])
except Exception as e:
Filed_List.append([i,Key[i]])
print 'Insert set Error:',e
return Filed_List

def Change_Set(self,R,KeyName,Key):
Filed_List=[]
NewKey=list(Key)
for i in range(0,len(NewKey)):
try:
R.sadd(KeyName,NewKey[i])
except Exception as e:
Filed_List.append(KeyName,NewKey[i])
print 'Insert set Error:',e
return Filed_List

def Change_Key(self,R,Keys):    #通过传递进来的Keys判断其值是属于哪种类型,从而调用不同的key的插入函数将该key插入进redis中
for i in Keys:
if isinstance(Keys[i],(str,int,float,long)):
print "The key %s type is string,will input new value:" %(i)
Bool = self.Change_String(R,i,Keys[i])
if Bool:
print "Update is ok,the key:%s New value:%s" %(i,Keys[i])
else:
print "Update is Filed,the Filed key %s value %s" %(i,Keys[i])
elif isinstance(Keys[i],list):
print "The key %s type is list,will input new value:" %(str(i))
Filed_List = self.Change_List(R,i,Keys[i])
if len(Filed_List) == 0:
print "Update is ok,the key:%s New value:%s" %(i,Keys[i])
else:
print "Update is Filed,the Filed List is %s" %(Filed_List)
elif isinstance(Keys[i],dict):
print "The key %s type is hash,will input new value:" %(str(i))
Filed_List = self.Change_Hash(R,i,Keys[i])
if len(Filed_List) == 0:
print "Update is ok,the key:%s New value:%s" %(i,Keys[i])
else:
print "Update is Filed,the Filed List is %s" %(Filed_List)
elif isinstance(Keys[i],set):
print "The key %s type is set,will input new value:" %(str(i))
Filed_List = self.Change_Set(R,i,Keys[i])
if len(Filed_List) == 0:
print "Update is ok,the key:%s New value:%s" %(i,Keys[i])
else:
print "Update is Filed,the Filed List is %s" %(Filed_List)
else:
print "The Key %s not match that support type.The support type:string,list,hash,set." %(i)
class BatchChangeKey:
def Batch_Change_Key(self,R,Keys,Value):
for i in R.keys(Keys):
self.ChangeKey=ChangeKey()
self.ChangeKey.Change_Key(R,{i:Value})

def main():
Pool=RedisPool()    #调用连接池类
R=Pool.Redis_Pool("192.168.0.25",6379,0)    #获取该连接池中的一个连接
#keys={'m':'huxianglin','e':[1,2,3],'c':{'z':'a','c':'b'},"abc:def:ghi":set(["a","b","c"])}
#changekey=ChangeKey()    #使用上面定义的Keys传递给该类,传递的keys要求是一个字典类型的数据
#changekey.Change_Key(R,keys)
batchchangekey=BatchChangeKey()    #调用批量修改的类,并将需要批量修改的key的一部分传递进去,可以通过R.keys(key)获取到能匹配的key的list,再遍历这个列表,将value传递进去
batchchangekey.Batch_Change_Key(R,"abc:defg:*" , "helloworld")
if __name__ == '__main__':
main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  demo python redis