#1. The Redis data is added and checked by Python, and the key is obtained1. Increased fromRedisImport*if __name__=="__main__": Try: #Create a Strictredis object and establish a link to the Redis serverSR =Strictredis ()#add key Py1, with a value of GJresult = Sr.set ('py1','GJ') #output response result, returns True if add succeeds, otherwise falsePrint(Result)exceptException as E:Print(e)2. By deleting fromRedisImport*if __name__=="__main__": Try: #Create a Strictredis object and establish a link to the Redis serverSR =Strictredis ()#sets the value of the key py1, modifies if the key already exists, and adds if the key does not existresult = Sr.delete ('py1') #outputs the corresponding result, returns the number of keys affected if the deletion succeeds, or returns 0Print(Result)exceptException as E:Print(e)3. Change fromRedisImport*if __name__=="__main__": Try: #Create a Strictredis object and establish a link to the Redis serverSR =Strictredis ()#sets the value of the py1, modifies if the key already exists, and adds if the key does not existresult = Sr.set ('py1','HR') #outputs the corresponding result, returns True if the operation succeeds, otherwise returns falsePrint(Result)exceptException as E:Print(e)4. Check fromRedisImport*if __name__=="__main__": Try: #Create a Strictredis object and establish a link to the Redis serverSR =Strictredis ()#gets the value of the key py1result = Sr.get ('py1') #The value of the output key that returns none if the key does not existPrint(Result)exceptException as E:Print(e)5. Get Key fromRedisImport*if __name__=="__main__": Try: #Create a Strictredis object and establish a link to the Redis serverSR =Strictredis ()#get all the keysresult =Sr.keys ()#output response result, all keys form a list, if no key points return empty listPrint(Result)exceptException as E:Print(e)#2. Example methods for different types of correspondence--string:set Setex mset append get mget--Key:keys exists type delete expire GetRange ttl--hash:hset hmset hkeys hget hmget hvals Hdel--list:lpush rpush linsert lrange lset lrem--set:sadd smembers Srem--zset:zadd zrange zrangebyscore zscore zrem zremrangebyscore
Redis's interaction with Python