1. Connect to Redis (two ways)
1 # Decode_responses=true: Resolving the obtained value type is a bytes byte problem 2 R = Redis. Redis (host='localhost', port='6379', db=0, decode _responses=true)
1 Pool = Redis. ConnectionPool (host='localhost', port=6379, db=0, decode_responses=True)2 r = Redis. Redis (Connection_pool=pool)
2. string-type String
1 #ex Expiration Time unit seconds s2R.set ('name','Jack', ex=20)3RST = R.get ('name')4 Print(RST)5 6 7Results:"Jack"
3. Listing Type list
1R.lpush ('Object',' One')2R.lpush ('Object',' Both')3R.lpush ('Object','three')4R.lpush ('Object',' Four')5R.lpush ('Object','Five')6R.lpush ('Object','Six')7ret = R.lrange ('Object', 0, 5)8 Print(ret[::-1], Len (ret))9 Ten OneResults: [' One',' Both','three',' Four','Five','Six'] 6
4. Hash type hashes
1R.hset ('User:info','name','Jack')2R.hset ('User:info',' Age', 20)3R.hset ('User:info','Phone','18712909999')4R.hset ('User:info','Email','[email protected]')5RST = R.hgetall ('User:info')6 Print(RST)7 8 9Result: {' Age':' -','Email':'[email protected]','name':'Jack','Phone':'18712909999'}
5. Collection type set
1R.sadd ('Set',' One')2R.sadd ('Set',' Both')3R.sadd ('Set','three')4res = R.smembers ('Set')5 Print(RES)6 7 8Result: {' Both',' One','three'}
6. Ordered collection type sorted set
1R.zadd ('Mark',' One', 1)2R.zadd ('Mark',' Both', 2)3R.zadd ('Mark','three'-R)4R.zadd ('Mark',' Four', 4)5R.zadd ('Mark','Five', 5)6result = R.zrange ('Mark', 0, 10)7 Print(Result)8 9 TenResults: [' One',' Both','three',' Four','Five']
Python 5 data types for REDIS operations