首页 > 编程语言 > 详细

Redis client Python usage

时间:2016-01-12 22:58:29      阅读:193      评论:0      收藏:0      [点我收藏+]

 

mport redis

r_server = redis.Redis(localhost) #this line creates a new Redis object and
                                    #connects to our redis server
r_server.set(test_key, test_value) #with the created redis object we can
                                        #submits redis commands as its methods
                                        
print previous set key  + r_server.get(test_key) # the previous set key is fetched

‘‘‘In the previous example you saw that we introduced a redis
data type: the string, now we will set an integer and try to
increase its value using redis object built-in methods‘‘‘

r_server.set(counter, 1) #set an integer to a key
r_server.incr(counter) #we increase the key value by 1, has to be int
print the counter was increased! + r_server.get(counter) #notice that the key is increased now

r_server.decr(counter) #we decrease the key value by 1, has to be int
print the counter was decreased! + r_server.get(counter) #the key is back to normal


‘‘‘Now we are ready to jump into another redis data type, the list, notice 
that they are exactly mapped to python lists once you get them‘‘‘

r_server.rpush(list1, element1) #we use list1 as a list and push element1 as its element

r_server.rpush(list1, element2) #assign another element to our list
r_server.rpush(list2, element3) #the same

print our redis list len is: %s% r_server.llen(list1) #with llen we get our redis list size right from redis

print at pos 1 of our list is: %s% r_server.lindex(list1, 1) #with lindex we query redis to tell us which element is at pos 1 of our list

‘‘‘sets perform identically to the built in Python set type. Simply, sets are lists but, can only have unique values.‘‘‘

r_server.sadd("set1", "el1")
r_server.sadd("set1", "el2")
r_server.sadd("set1", "el2")

print the member of our set are: %s% r_server.smembers("set1")

‘‘‘basically our redis client can do any command supported by redis, check out redis documentation for available commands for your server‘‘‘

 

Redis client Python usage

原文:http://www.cnblogs.com/skying555/p/5125750.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!