python连接redis

redis的安装可以参考前一篇,本文采用python2.7.4和python3.3.1作为测试,我的系统是ubuntu,默认的python是python2。

先安装redis-py

wget https://github.com/shuxiang/redis-py/archive/master.zip
unzip master.zip > /dev/null
cd redis-py-master/
sudo python setup.py install
sudo python3 setup.py install

需要注意的是,python2和python3需要分别安装redis-py,否则另一个python不能用。然后看一下python2.7.4的代码

# 20140118, redis_test.py

import sys
print(sys.version)
import platform
print(platform.python_version())

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r2 = redis.StrictRedis(host='localhost', port=6379, db=0)

print(">>>>>>>>>>TEST REDIS LIST>>>>>>>>>>")
r.lpush("list1",3)
r.lpush("list1",2)
r.lpush("list1",1)
r.rpush("list1",4)
r2.rpush("list1",5)
print(r.lrange("list1",0,-1))
print(r.llen("list1"))
r.ltrim("list1",0,2)
print(r.lrange("list1",0,-1))
print(r.lindex("list1",1))
print("TEST " + (r.type("list1")) + " END")
r.delete("list1")

print(">>>>>>>>>>TEST REDIS STRING>>>>>>>>>>")
r.set("str1", '1234567890')
print(r2.get("str1"))
print("TEST " + (r.type("str1")) + " END")
r.delete("str1")

print (">>>>>>>>>>TEST REDIS SET>>>>>>>>>>")
r.sadd("set1","222")
r.sadd("set1","111")
r.sadd("set1","333")
print(r.scard("set1"))
print(r.smembers("set1"))
r2.sadd("set2","444")
r2.sadd("set2","333")
r2.sadd("set2","555")
print(r.sunion("set1","set2"))
print(r.sinter("set1","set2"))
print("TEST " + (r.type("set1")) + " END")
r.delete("set1")
r.delete("set2")

print(">>>>>>>>>>TEST REDIS ZSET>>>>>>>>>>")
r.zadd("sort1",2,"222")
r.zadd("sort1",1,"111")
r.zadd("sort1",3,"333")
print(r.zrange("sort1",0,-1))
print(r.zrangebyscore("sort1",2,3))
print(r.zrangebyscore("sort1",2,-1))
print("TEST " + (r.type("sort1")) + " END")
r.delete("sort1")

print(">>>>>>>>>>TEST REDIS HASH TABLE>>>>>>>>>>")
r.hset("hash1","key1",1)
r.hset("hash1","key2",2)
r.hset("hash1","key3",3)
print(r.hget("hash1","key2"))
print(r.hkeys("hash1"))
print(r.hexists("hash1","key4"))
r.hdel("hash1","key2")
print(r.hget("hash1","key2"))
print(r.hkeys("hash1"))
print("TEST " + (r.type("hash1")) + " END")
r.delete("hash1")

print(r.keys())

运行结果如下

2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3]
2.7.4
>>>>>>>>>>TEST REDIS LIST>>>>>>>>>>
['1', '2', '3', '4', '5']
5
['1', '2', '3']
2
TEST list END
>>>>>>>>>>TEST REDIS STRING>>>>>>>>>>
1234567890
TEST string END
>>>>>>>>>>TEST REDIS SET>>>>>>>>>>
3
set(['111', '222', '333'])
set(['555', '111', '222', '333', '444'])
set(['333'])
TEST set END
>>>>>>>>>>TEST REDIS ZSET>>>>>>>>>>
['111', '222', '333']
['222', '333']
[]
TEST zset END
>>>>>>>>>>TEST REDIS HASH TABLE>>>>>>>>>>
2
['key1', 'key2', 'key3']
False
None
['key1', 'key3']
TEST hash END
[]

还有python3.3.1的代码

# 20140118, redis_test_python3.py

import sys
print(sys.version)
import platform
print(platform.python_version())

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r2 = redis.StrictRedis(host='localhost', port=6379, db=0)

print(">>>>>>>>>>TEST REDIS LIST>>>>>>>>>>")
r.lpush("list1",3)
r.lpush("list1",2)
r.lpush("list1",1)
r.rpush("list1",4)
r2.rpush("list1",5)
print(r.lrange("list1",0,-1))
print(r.llen("list1"))
r.ltrim("list1",0,2)
print(r.lrange("list1",0,-1))
print(r.lindex("list1",1))
print('TEST %s END' % (r.type("list1")))
r.delete("list1")

print(">>>>>>>>>>TEST REDIS STRING>>>>>>>>>>")
r.set("str1", '1234567890')
print(r2.get("str1"))
print('TEST %s END' % (r.type("str1")))
r.delete("str1")

print (">>>>>>>>>>TEST REDIS SET>>>>>>>>>>")
r.sadd("set1","222")
r.sadd("set1","111")
r.sadd("set1","333")
print(r.scard("set1"))
print(r.smembers("set1"))
r2.sadd("set2","444")
r2.sadd("set2","333")
r2.sadd("set2","555")
print(r.sunion("set1","set2"))
print(r.sinter("set1","set2"))
print('TEST %s END' % (r.type("set1")))
r.delete("set1")
r.delete("set2")

print(">>>>>>>>>>TEST REDIS ZSET>>>>>>>>>>")
r.zadd("sort1",2,"222")
r.zadd("sort1",1,"111")
r.zadd("sort1",3,"333")
print(r.zrange("sort1",0,-1))
print(r.zrangebyscore("sort1",2,3))
print(r.zrangebyscore("sort1",2,-1))
print('TEST %s END' % (r.type("sort1")))
r.delete("sort1")

print(">>>>>>>>>>TEST REDIS HASH TABLE>>>>>>>>>>")
r.hset("hash1","key1",1)
r.hset("hash1","key2",2)
r.hset("hash1","key3",3)
print(r.hget("hash1","key2"))
print(r.hkeys("hash1"))
print(r.hexists("hash1","key4"))
r.hdel("hash1","key2")
print(r.hget("hash1","key2"))
print(r.hkeys("hash1"))
print('TEST %s END' % (r.type("hash1")))
r.delete("hash1")

print(r.keys())

运行结果如下

3.3.1 (default, Sep 25 2013, 19:29:01) 
[GCC 4.7.3]
3.3.1
>>>>>>>>>>TEST REDIS LIST>>>>>>>>>>
[b'1', b'2', b'3', b'4', b'5']
5
[b'1', b'2', b'3']
b'2'
TEST b'list' END
>>>>>>>>>>TEST REDIS STRING>>>>>>>>>>
b'1234567890'
TEST b'string' END
>>>>>>>>>>TEST REDIS SET>>>>>>>>>>
3
{b'222', b'111', b'333'}
{b'222', b'555', b'444', b'111', b'333'}
{b'333'}
TEST b'set' END
>>>>>>>>>>TEST REDIS ZSET>>>>>>>>>>
[b'111', b'222', b'333']
[b'222', b'333']
[]
TEST b'zset' END
>>>>>>>>>>TEST REDIS HASH TABLE>>>>>>>>>>
b'2'
[b'key1', b'key2', b'key3']
False
None
[b'key1', b'key3']
TEST b'hash' END
[]

可以发现,python2和python3的输出还是有一些不同的,但这些不同应该是print函数调用__str__的时候引起的差异,程序本身的功能应该没有什么差异。

同时可以对比一下前一篇中ruby的redis api函数,可以看出与python基本是一致的,真的是很难得。

posted @ 2014-01-18 11:10  valleylord  阅读(5513)  评论(0编辑  收藏  举报