Python Operations Redis Database (non-relational database, K-V)

Source: Internet
Author: User
Tags sessions

Database:
1. Relational database
    • Table structure
    2. Non-relational database NoSQL (k-v  speed), commonly used when the following three kinds:
    • Memcache exists in memory
    • Redis exists in memory
    • Mangodb data is still present on disk
The data types in Redis are in string and hash types, and the following are mostly operations on Redis.

One, String type
R = Redis. Redis (host='localhost', port=6379,db=3) R.set ('Nancy2','201801211505')#Set DataPrint(R.get ('Nancy'))#b ' 201801211505 'Print(R.get ('Unexsitkey'))#->nonePrint(R.get ('Nancy1'). Decode ())#the data extracted from Redis is bytes type, all of which are converted to strings using the Decode () method->201801211505R.delete ('Nancy')#Delete aR.setex ('nancy_session','aaabb66',' -')#You can specify the expiration time of the key, the last unit, the unit is the secondR.set ('redis:nancy','python000') R.set ('Redis:nancy1','redis00123') R.set ('redis1:kate','REDIS11')Print(R.keys ())#byte type, list all keysPrint(R.keys ('redis*'))#key,byte types starting with txz,-> [b ' redis:nancy1 ', b ' redis:nancy ', B ' redis1:kate ']Print(R.type ('redis:nancy'))#B ' String '

Note: The set get delete Setex are all for string types in Redis

second, the type of hash
R.hset ('Sessions','Lily','167890') R.hset ('Sessions','Kate','34901') Value= R.hget ('Sessions','Nancy')#Get DataPrint(value) value1= R.hget ('Session','Unexsit')#does not exist return nonePrint(value1) Redis_data= R.hgetall ('Sessions')#get all the data in the hash type, the data type is the dictionaryAll_date = {} forKvinchRedis_data.items (): K=K.decode () v=V.decode () all_date[k]=vPrint(Redis_data)#Dictionary of byte type, {b ' Kate ': B ' 34901 ', B ' Lily ': B ' 167890 '}Print(R.type ('Sessions'))#get the type of key, B ' hash '

Python Operations Redis Database (non-relational database, K-V)

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.