写在前面python
首先声明,这是为了学习python对redis操做而写的一个小demo,包括了这几天网站找到的一些资料,综合总结出来一些东西,最后附上我写的一个用python操做redis的一个demo:
redis
模块安装shell
python提供了一个模块redis-py来使咱们很方便的操做redis数据库,安装该模块也很简单,直接使用pip安装就行,命令以下:数据库
pip install redis
安装完以后,使用import调用一下就能知道是否安装成功,在python界面下输入import redis,若是不报错,那么该模块就算安装成功了。app
模块安装成功后,就能够建立redis链接了,接下来学习怎样建立redis链接:socket
redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令(好比,SET命令对应与StrictRedis.set方法)。Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。 简单说,官方推荐使用StrictRedis方法。ide
这里不推荐使用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()