Redis Database--python Interaction

Source: Internet
Author: User
Tags download redis instance method virtual environment redis server

-  Installation-Installation package ① Enter virtual Environment py2_db, networking installation package Redispip install redis② Chinese official website (http://redis.cn/clients.html) Download Redis Source Installation unzip redis -py -master.zipcd redis -py -masterpython setup.py  install--Calling module From redis import *- strictredis object Method--Creating an object from Init, specifying the parameter host, Port is connected to the specified server and port, host defaults to localhost,port default to 6379--depending on the type, the owning instance method can be called, corresponding to the Redis command learned earlier, the method requires parameters that match the parameters of the command. --Example Method:--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- string-Increase from  redis import *if __name__ ==  "__main__":    try:         #  Create StrictRedis objects, establishing links with Redis servers         sr = strictredis ()          #  add key py1 with a value of Gj        result  = sr.set (' py1 ',  ' GJ ')         #  output response results, Returns True if add succeeds, otherwise returns False        print (result)      Except exception as e:        print (e)-  string-get from redis import *if __name__ ==  "__main__":     try:        #  Create Strictredis object, link with Redis server          sr = strictredis ()         #   Gets the value of the key py1         result = sr.get (' py1 ')          #  the value of the output key,Returns None        print (Result) If the key does not exist     except  Exception as e:        print (e)- string-modification from  redis import *if __name__ ==  "__main__":    try:         #  Create a Strictredis object and link to a Redis server          sr = strictredis ()         #  set the value of Py1, If the key already exists, modify it and add         result = sr.set if the key does not exist (' py1 ',  ' HR ')         #  outputs the corresponding result, returns True if the operation succeeds, or returns false         print (Result)     except Exception as e:         print (e)- string-Delete from redis import *if __ name__ ==  "__main__":    try:        #  Create Strictredis objects, Establish a link to a Redis server         sr = strictredis ()          #  sets the value of the key py1, modifies if the key already exists, and adds it if the key does not exist          result = sr.delete (' py1 ')         #  outputs the corresponding results, Returns the number of keys affected if the deletion succeeds, or returns 0        print (result)      Except exception as e:        print (e)-  get key from  redis import *if __name__ ==  "__main__":    try:         #  Create a Strictredis object and link to a Redis server          sr = strictredis ()         #  get all the keys        &Nbsp;result = sr.keys ()         #  output response result, all keys form a list, Returns an empty list if no key points         print (result)     except  Exception as e:        print (e)

Redis Database--python interaction

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.