Python operation of the Redis string operation

Source: Internet
Author: User

# __author__ = ' STEVEN ' import redis,time# mode 1, Direct link operation # r = Redis. Redis (host= ' 192.168.43.22 ', port=6379) # r.set (' name ', ' Lisi ') # Print (R.get (' name '). Decode ()) # mode 2, create process Pool poll = Redis. ConnectionPool (host= ' 192.168.43.22 ', port=6379) #选择进程池r = Redis. Redis (Connection_pool=poll) # #set () implementation defines the string name, value, expiration time (ex[s],ps[ms],nx[true if there is a new],xx[true if it exists, overwriting him) # R.set (' Age ', ' ex=2 ', ' # print ' (R.get (' age '). Decode ()) # Time.sleep (2) # Print ((R.get ("age"). Decode ()) If R.get (' age ')! = None Else ' None ') # Nx[true if not present in the new], there is also no change # r.set (' name ', ' Lisi ', nx=true) # Print (R.get (' name ')) # Xx[true If it exists then overwrite him], does not exist nor new # R.set (' name ', ' Man ', Xx=true) # Print (R.get (' name ')) #setnx () if key already exists, return false# print (r.setnx (' name2 ', ' Huhu ')) # Print (R.get (' name2 ')) #setex () sets the value of the key corresponding to string, and specifies the validity period for this key value # R.setex (' Age1 ', ' Max ', ') # Print (R.get (' Age1 ')) # Time.sleep (+) # print (R.get (' Age1 ') # SETRANGE () sets the substring of the value of the specified key # r.set (' name ', ' Man ') # r.setrange (' name ', 1, ' e ') # Print (r.get (' name ')) # Mset () set multiple key values at once, successful return OK means all values are set, failed to return 0 means no value is set. # R.mset ({' mes1 ': ' Nihao ', ' mes2 ': ' Woyehao '}) # Print (R.mget ([' mes1 ', ' Mes2 ')) # MSETNX () Set the value of multiple keys at once, successful return OK means all values are set , a failure return of 0 means that no value is set, but the key that already exists is not overwritten. #都不存在时新建 # Print (r.msetnx ({' Mes11 ': ' Nihao ', ' mes21 ': ' Woyehao1 '}) # Print (R.mget ([' Mes11 ', ' Mes21 ')) # Getset () set key Value and returns the old value of key # print (R.getset (' Mes11 ', ' Nihaoma ')) # Print (R.get (' Mes11 ')) # GetRange () Gets the substring of the value of the specified key. # Print (R.getrange (' Mes11 ', 1,3)) # INCR () makes a Gaga operation on the value of key and returns the new value. Note INCR A value that is not int returns an error, incr a nonexistent key, setting key to # Print (R.INCR (' IDs ') # Incrby () is similar to INCR, plus the specified value, key does not exist when the key is set , and think that the original value is 0# print (R.incrby (' IDs ', 3)) # DECR () to the value of the key is a subtraction operation, decr a key does not exist, then set key to -1# print (R.DECR (' lib ')) # Appen D () Appends value to the string value of the specified key, returning the length of the new string value # print (R.append (' mes11 ', ' 123 ')) # Print (R.get (' Mes11 ')) # strlen () takes the length of the value of the specified key # r.set (' info ', ' 1234 ') # Print (R.strlen (' info '))

  

Python operation of the Redis string operation

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.