Redis Client Python Usage

Source: Internet
Author: User
Tags redis server

mport Redisr_server= Redis. Redis ('localhost')#This line creates a new Redis object and                                    #connects to our Redis serverR_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 redisdata type:the string, now we'll 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 keyR_SERVER.INCR ('counter')#We increase the key value by 1, have to is intPrint 'The counter was increased!'+ R_server.get ('counter')#Notice the key is increased nowR_SERVER.DECR ('counter')#We decrease the key value by 1, have to is intPrint 'The counter was decreased!'+ R_server.get ('counter')#The key is back to normal" "Now we is ready-to-jump-to- another Redis data type, the list, notice that they is exactly mapped to Python lists Once you get them" "R_server.rpush ('List1','element1')#we use the List1 as a list and push element1 as its elementR_server.rpush ('List1','Element2')#assign another element to our listR_server.rpush ('List2','Element3')#The samePrint 'Our Redis list len is:%s'% R_server.llen ('List1')#With Llen we get my redis list size right from RedisPrint 'At Pos 1 of our list is:%s'% R_server.lindex ('List1', 1)#With lindex, we query Redis to tell us which element are at POS 1 for our list" "sets perform identically to the built in Python set type. Simply, sets is lists but the can only has unique values." "R_server.sadd ("Set1","El1") R_server.sadd ("Set1","El2") R_server.sadd ("Set1","El2")Print 'The member of our set is:%s'% R_server.smembers ("Set1")" "basically our Redis client can does any command supported by Redis, check out Redis documentation for available commands For your server" "

Redis Client Python Usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.