Database:
1. Relational database
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)