Redis 提供了很多丰富的命令,最常见的如get ,set , multiple-get . multiple-set 命令
另外提供了list操作命令 ,list 实际上是一个堆栈 提供lpush 和lpop方法
Set 为集合操作 可以进行交并等运算
ZSet为有序集合,传入值的时候可以传入一个权重 ,以控制此值在整个set中的位置
Pipeline 维持一个事务, 当没有执行excute时, 此事务内的所有操作都没有被真正提交
具体使用见代码. 调用client.info()的时候出错 , 怀疑可能是client端的bug
print "---Key Size---"
print str(client.dbsize())
print "---Key Size---"
#Returns the Redis server info
'''
print "---info---"
infoDict = client.info()
myIter = infoDict.iteritems()
while True :
try :
item_tuple = myIter.next()
print repr(item_tuple)
except StopIteration :
break
print "---info---"
'''
#Get & Set
print "---Get & Set---"
client.set("demo", "demo_value")
print str(client.get("demo"))
print "---Get & Set---"
#Append
print "---Append---"
client.append("demo", "_append_value");
print str(client.get("demo"))
print "---Append---"
#incr
print "---incr---"
client.set("count", 0)
print str(client.get("count"))
client.incr("count", 1)
print str(client.get("count"))
client.incr("count", 1)
print str(client.get("count"))
print "---incr---"
#decr
print "---decr---"
client.decr("count", 1)
print str(client.get("count"))
print "---decr---"
#List
print "---List---"
client.lpush("list", "one");
client.lpush("list", "two");
print ("length:" + str(client.llen("list")))
print str(client.lrange("list", 0, -1))
print str("pop:" +client.lpop("list"))
print str(client.lrange("list", 0, -1))
client.ltrim("list", 2, 1)
print "---List---"
#Set
print "---Set---"
client.sadd("set_1", "1-one");
client.sadd("set_1", "1-two");
client.sadd("set_1", "common");
print ("length:" + str(client.scard("set_1")))
print str(client.smembers('set_1') )
client.srem("set_1", "1-two")
print str(client.smembers('set_1') )
client.sadd("set_2", "2-one");
client.sadd("set_2", "2-two");
client.sadd("set_2", "common");
print str(client.smembers('set_2') )
set = []
set.append("set_1")
print str(client.sinter(set,"set_2"))
print str(client.sunion(set,"set_2"))
client.sinterstore("set_3" ,set,"set_2")
print "inter :" + str(client.smembers("set_3"))
client.sunionstore("set_4" ,set,"set_2")
print "union :" + str(client.smembers("set_4"))
diffSet = []
diffSet.append("set_1")
diffSet.append("set_2")
print str(client.smembers('set_1') )
print str(client.smembers('set_2') )
print "diff :" + str(client.sdiff(diffSet))
client.sdiffstore("set_5", diffSet);
print "diff :" + str(client.smembers('set_5'))
print "---Set---"
print "---zset---"
client.zadd("zset", "three", 3);
client.zadd("zset", "one", 1);
client.zadd("zset", "two", 2);
print str(client.zrange('zset', 0, -1, 0))
print str(client.zrangebyscore('zset', 1, 2))
print "---zset---"
print "---transactions---"
transactions = client.pipeline()
transactions.sadd("transactions_set", "valueone");
transactions.sadd("transactions_set", "valuetwo");
print "transactions show :" + str(transactions.smembers("transactions_set"))
print "client show :" + str(client.smembers("transactions_set"))
transactions.reset()
print "---after rollback---"
print "transactions show :" + str(transactions.smembers("transactions_set"))
print "client show :" + str(client.smembers("transactions_set"))
print "---after rollback---"
transactions.sadd("transactions_set", "valueone");
transactions.sadd("transactions_set", "valuetwo");
transactions.execute()
print "---after execute---"
print "transactions show :" + str(transactions.smembers("transactions_set"))
print "client show :" + str(client.smembers("transactions_set"))
print "---after execute---"
print "---transactions---"
输出如下
---Key Size---
1237750
---Key Size---
---Get & Set---
demo_value
---Get & Set---
---Append---
demo_value_append_value
---Append---
---incr---
0
1
2
---incr---
---decr---
1
---decr---
---List---
length:2
['two', 'one']
pop:two
['one']
---List---
---Set---
length:4
set(['one', '1-one', '1-two', 'common'])
set(['1-one', 'common', 'one'])
set(['2-two', '2-one', 'common'])
set(['common'])
set(['2-two', '1-one', '2-one', 'common', 'one'])
inter :set(['common'])
union :set(['2-two', '1-one', '2-one', 'common', 'one'])
set(['1-one', 'common', 'one'])
set(['2-two', '2-one', 'common'])
diff :set(['1-one', 'one'])
diff :set(['1-one', 'one'])
---Set---
---zset---
['one', 'two', 'three']
['one', 'two']
---zset---
---transactions---
transactions show :<redis.clientjar.client.Pipeline object at 0x00C069C0>
client show :set([])
---after rollback---
transactions show :<redis.clientjar.client.Pipeline object at 0x00C069C0>
client show :set([])
---after rollback---
---after execute---
transactions show :<redis.clientjar.client.Pipeline object at 0x00C069C0>
client show :set(['valueone', 'valuetwo'])
---after execute---
---transactions---
另外,Redis还提供了publish/subscribe 机制
发布端代码:
client.publish("test channel", "publish message")
print "---subscribe---"
订阅端代码:
for m in client.listen():
print(m)
先运行订阅端进行监听,接着运行发布端发布消息 ,打印出如下信息
{'pattern': None, 'type': 'message', 'channel': 'test channel', 'data': 'publish message'}