Redis's interaction with Python

Source: Internet
Author: User
Tags redis server

#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

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.