Installing Python-redis
Pip Install Redis
Python Operation Redis
#To import a Redis class from a Redis package fromRedisImportRedis#initializing a Redis instanceCache = Redis (host='10.2.2.120', port='6379')#Manipulating StringsCache.set ('username','ABC') Cache.delete ('username')#List OperationsCache.lpush ('Books','Java') Cache.lpush ('Books','python') Cache.lpush ('Books','PHP')Print(Cache.lrange ('Books', 0,-1))#operation of the collectionCache.sadd ('Team','Blue') Cache.sadd ('Team','Yellow') Cache.sadd ('Team','Red')Print(Cache.smembers ('Team'))#operation of HashingCache.hset ('website','Baidu','www.baidu.com') Cache.hset ('website','Google','www.google.com')Print(Cache.hgetall ('website'))#operation of the transactionPip =cache.pipeline () pip.set ('Usernmae','Heboan') Pip.set ('Password','123456') Pip.execute ()#Publish and subscribe (publish a subscription to a different file)#Subscribe to MessagesPS =cache.pubsub () ps.subscribe ('Email') whileTrue: forIteminchPs.listen ():Print(item)#Publish a message forXinchRange (3): Cache.publish ('Email','[email protected]')
Here is just a list of some basic operations that are actually the same as the command line
Python Operation Redis