function Python to operate Redis

Source: Internet
Author: User

Redis is a database of key-value structures, and the format of value can be String,set (), List,map collection (that is, Python dict), sorted set (ordered collection)

1. Connect to Redis Database

Description Episode: Redis and Strictredis, the latter being the official recommended use

Import Redis

R=redis. Redis (host= ' ID address ', port=6379,password= ', db=12) #指定连接redis的端口, port number, and which database # connection redis# Add and delete changes

Set function:
R.set (' abc ', ' 12345686 ') #50表示50s之后连接失效, when there is no key, add the value of the key, change its value when it exists, and return a value of true
Print (R.set (' abc ', ' 12345686 ')) #打印返回值为True
Print (R.get (' abc ')) #获取到key的值

Setnx function:
R.setnx (' 1314520 ', ' Jidad ') #如果值不存在则新增一个, if present, do not modify the original value

Print (R.get (' 1314520 ')) #获取到1314520的值

Delete function: Remove key and ignore if key does not exist
R.delete (' abc ') #指定一个key, delete the key and value if key exists return 1, if not present then return 0
Print (R.delete (' abc ')) #返回值0或者1

Binary: Byte type

Decode function: Turn byte type to string

Encode function:

To turn a string type to a byte type

Str= ' saksj Jadja '
Print (Str.encode ())


Keys function: Gets all keys, does not exist then returns to null
Print (R.keys (' *o ')) #获取到所有的key值
Printing results:


exists function:
Print (r.exists (' abc ')) #判断key是否存在. Exists as true and does not exist for Fasle

Flushall function:
R.flushall () #清空redis里所有的数据
Flushdb function:
R.FLUSHDB () #清空当前数据库里面的所有的key

Second, the hash type key
Hash type: Can be understood as a dictionary nested dictionary

function Python to operate Redis

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.