Redis Operations for Python

Source: Internet
Author: User
Tags install redis

Redis

is a high-performance Key-value database, which is a non-relational database. The following three features are available:

    • Redis supports data persistence, which saves data in memory on disk and can be loaded again for use when restarting.
    • Redis not only supports simple key-value types of data, but also provides storage of data structures such as List,set,zset,hash.
    • Redis supports backup of data, that is, Master-slave mode of data backup.

1, Redis installation.

In the cmd command window, enter: Pip install Redis

2, connect Redis.

R = Redis. Redis (host= ' 127.0.0.1 ', password= ' hk139bc&* ', db=1,port=6379)

Description: Host is the IP address, password is the Redis password, DB connection after the use of the database, redis default has 15 databases, port is the port number, the default is 6379

3. Redis Delete and change, for the operation of string type.

1), a new value in the database:

R.set ('qianqian',' handsome!  '# A new value is added to the database. K is Qianqian,value is handsome, the redis inside has this k's words to modify the value, does not have the words to increase the k, and the value
R.set (' Xiehong:qianqian ', ' Handsome! ') #数据库里面新增一个值, if there is a colon inside K, then a folder named after the colon will be generated in the Redis after successful insertion

2), get the value of K

Print (R.get ('xiehong:qianqian')) # Get K is the value of Xiehong:qianqian, the default return result is the binary Print (R.get ('xiehong:qianqian'). Decode ()) # converting binary results to Chinese # The result is:b'\xe5\xb8\x85\xef\xbc\x81' handsome! 

3), delete K and value

R.delete ('xiehong:qianqian')# after deletion, the corresponding K and value are deleted

4), set the expiration time of key

R.setex ('python_123',' hahaha ', ')  #  Set the expiration time of key, the last parameter is the second

5), get all the keys

R.keys ('*xxx*'))# gets all the keys

4, hash type (hash) operation.

The value of a hash type is a nested dictionary.

1), add K and value

R.hset ('Xinxin','New New','1M8 100w Deposit') R.hset ('Xinxin','Red Red','How many lessons can be wasted in the years') R.hset ('Xinxin','Qian','I wish I could walk through you and see the Truth')
The above key is Xinxin,value is the following data, log on to Redis on the operation results of the above code:

2), to obtain value, you need to make a large key and small key to obtain the corresponding value, for example:

Print (R.hget ('xinxin',' new '). Decode ())  # specify big key and small key to get corresponding data # Gets the result:1m8 100w Deposit

3), get all the key and value

Print (R.hgetall ('xinxin'))  # get all the K and V inside

4), delete key

R.hdel ('stu_info','gyx')  # Delete the specified key R.delete ('stu_info')  # Delete entire key

5), set the expiration time of the first key

R.expire ('xinxin'# first key set expiration time

5. Supplementary string Conversion

s=' hehe '# turn the string into binary HWT = b'sdfsdfsdf'  Hwt.decode ()  # Turn the bytes type into a string



Redis Operations for Python

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.