【redis】Python客户端redis-py

1.Python客户端redis-py

获取redis-py 
redis-py的基本使用方法
redis-py的pipeline的使用
redis-py的Lua脚本使用。

http://redis.io/clients#python 

2.获取redis-py

(1)pip install redis 
(2)easy_install redis 
(3)源码安装。
wget https://github.com/andymccurdy/redis-py/archive/2.10.5.zip 
unzip 2.10.5.zip
cd redis-py-2.10.5
python setup.py install 

3.使用redis-py 

python 
>>> import redis
>>> client=redis.StrictRedis(host='192.168.1.7',port=6379)
>>> client.set("key","python-redis")
True
>>> client.get("key")
'python-redis'
>>> 


--完美使用redis-py工具,让Python能够操作redis;
--实现redis的自动化运维。
vi 1.py 

import redis
client=redis.StrictRedis(host='192.168.1.7',port=6379)
key="hello"
setResult=client.set(key,"python-redis")
print setResult
value=client.get(key)
print "key:"+key+",value:"+value 


[root@oracle1 ~]# python 1.py
True
key:hello,value:python-redis

4.使用redis-py操作redis的五种数据结构。

import redis 
client=redis.StrictRedis(host='192.168.1.7',port=6379)
#1.string 
client.set("hello","world")
client.get("hello")
client.incr("counter")
#2.hash 
client.hset("myhash","f1","v1")
client.hset("myhash","f2","v2")
client.hgetall("myhash")
#3.list 
client.rpush("mylist","1")
client.rpush("mylist","2")
client.rpush("mylist","3")
client.lrange("mylist",0,-1)
#4.set 
client.sadd("myset","a")
client.sadd("myset","b")
client.sadd("myset","a")
client.smembers("myset")
#5.zset 
client.zadd("myzset","99","tom")
client.zadd("myzset","66","peter")
client.zadd("myzset","33","james")
client.zrange("myzset",0,-1,withscores=True)

--python命令行输出结果如下。
>>> import redis 
>>> client=redis.StrictRedis(host='192.168.1.7',port=6379)
>>> client.set("hello","world")
True
>>> client.get("hello")
'world'
>>> client.incr("counter")
5
>>> client.hset("myhash","f1","v1")
0L
>>> client.hset("myhash","f2","v2")
0L
>>> client.hgetall("myhash")
{'f1': 'v1', 'f2': 'v2'}
>>> client.rpush("mylist","1")
10L
>>> client.rpush("mylist","2")
11L
>>> client.rpush("mylist","3")
12L
>>> client.lrange("mylist",0,-1)
['1', '2', '3', '1', '2', '3', '1', '2', '3', '1', '2', '3']
>>> client.sadd("myset","a")
0
>>> client.sadd("myset","b")
0
>>> client.sadd("myset","a")
0
>>> client.smembers("myset")
set(['a', 'b'])
>>> client.zadd("myzset","99","tom")
0
>>> client.zadd("myzset","66","peter")
0
>>> client.zadd("myzset","33","james")
0
>>> client.zrange("myzset",0,-1,withscores=True)
[('james', 33.0), ('peter', 66.0), ('tom', 99.0)]
>>> 
>>> 

5.redis-py中pipeline的使用方法

(1)引入依赖库
import redis 
client=redis.StrictRedis(host='192.168.1.7',port=6379)
(2)生成pipeline;
--不使用事务
pipeline=client.pipeline(transaction=False)
(3)将命令封装到pipeline中。
--此时并没有执行,而是排队。
pipeline.set("hello","world")
pipeline.incr("counter")
(4)result=pipeline.execute() 

vi 3.py
import redis 
client=redis.StrictRedis(host='192.168.1.7',port=6379)
pipeline=client.pipeline(transaction=False)
pipeline.set("hello","world2")
pipeline.incr("counter")
result=pipeline.execute() 
print(client.get("hello"))

[root@oracle1 ~]# python 3.py
world2

--将用redis-py的pipeline实现mdel功能。
vi 4.py 
import redis 
def mdel(keys):
  client=redis.StrictRedis(host='192.168.1.7',port=6379)
  pipeline=client.pipeline(transaction=False)
  for key in keys:
    print pipeline.delete(key)
  return pipeline.execute() 
  print(client.get("hello"))

mdel("hello" "hello2" "hello3") 

--可以看到应该是使用了连接池。
[root@oracle1 ~]# python 4.py hello
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=192.168.1.7,port=6379,db=0>>>

6.redis-py中的Lua脚本使用方法。

eval(String script,int keyCount,String...params)
script_load(String script)
evalsha(String sha1,int keyCount,String..params)

script:Lua脚本的内容。
keyCount:键的个数 
params:相关参数KEYS和ARGV 

vi 5.py 
import redis 
client=redis.StrictRedis(host='192.168.1.7',port=6379)
script="return redis.call('get',KEYS[1])"
print client.eval(script,1,"hello")

[root@oracle1 ~]# python 5.py
world2

evalsha函数用来执行脚本的哈希值:
scriptSha:脚本的sha1;
keyCount:键的个数。
params:相关参数KEYS,ARGV;

vi 6.py 
import redis 
client=redis.StrictRedis(host='192.168.1.7',port=6379)
script="return redis.call('get',KEYS[1])"
scriptSha=client.script_load(script)
print client.evalsha(scriptSha,1,"hello")

[root@oracle1 ~]# python 6.py
world2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值