Redis interacts with Python

Source: Internet
Author: User
Tags delete key download redis virtual environment install redis redis server

Install package

There are 3 ways to install Redis https://github.com/andymccurdy/redis-py

    • The first type: into the virtual environment, the United? Install the package Redis

      Pip Install Redis

    • The second type: into the virtual environment, the United installation package Redis

      Easy_install Redis

    • The third kind: to the official?-Client Download Redis package source code, so that the source installation

      Step-by-step execution of wget https://github.com/andymccurdy/redis-py/archive/master.zip
      Unzip Master.zip
      CD Redis-py-master
      sudo python setup.py install

Tuning Module
    • Introduction module

      From Redis Import *

    • The Strictredis object (strict strict) is provided in this module, which is used to connect Redis server and provide different methods according to different types. Interactive operation
Strictredis object? method
    • Create an object from init, specify the parameter host, port with the specified server and end connection, host default is localhost,port default is 6379,db default is 0
sr = Strictredis (host= ' localhost ', port=6379, db=0) abbreviated Sr=strictredis ()
    • Depending on the type, have a different instance? The method can be adjusted, corresponding to the Redis command of the pre-learning, the parameters required by the method and the parameters of the command.
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
Get ready
    • Create a Redis directory on the desktop
    • Open the Redis directory using Pycharm
    • Create a redis_string.py file
From Redis import *if __name__== "__main__":    try:        #创建StrictRedis对象, built with Redis server connection        Sr=strictredis ()    Except Exception as E:        print (e)
string-Increase
    • Method set, add key, value, returns True if add succeeds, False if add failed
    • Write the following code
From Redis import *if __name__== "__main__":    try:        #创建StrictRedis对象, built with Redis server connection        Sr=strictredis ()        #添加键name, the value is Skylark        result=sr.set (' name ', ' Skylark ')        #输出响应结果, returns True if add succeeds, otherwise false        print ( Result)    except Exception as E:        print (e)
string-get
    • A. Get, add the value corresponding to the key, return the corresponding value if the key exists, or none if the key does not exist
    • Write the following code
From Redis import *if __name__== "__main__":    try:        #创建StrictRedis对象, built with Redis server connection        Sr=strictredis ()        # Gets the value of the key name        , result = Sr.get (' name ')        #输出键的值, and returns none if the key does not exist        print (result)    except Exception as E:        Print (e)
string-modification
    • Method set, if the key already exists in the change, if the key does not exist in the add

Write the following code

From Redis import *if __name__== "__main__":    try:        #创建StrictRedis对象, built with Redis server connection        Sr=strictredis ()        #设置键name的值, if the key already exists, then go in? Modify, if the key does not exist then go in? Add        result = Sr.set (' name ', ' Skylark ')        #输出响应结果, if the operation succeeds, returns True, Otherwise returns false        print (result)    except Exception as E:        print (e)
string-Delete
    • Method Delete, delete key and corresponding value, return the affected key number if Delete succeeds, or return 0
    • Write the following code
From Redis import *if __name__== "__main__":    try:        #创建StrictRedis对象, built with Redis server connection        Sr=strictredis ()        # Set the value of the key name, if the key already exists, in the change, if the key does not exist then into? Add        result = Sr.delete (' name ')        #输出响应结果 If the delete succeeds returns the number of keys affected, or 0        print ( Result)    except Exception as E:        print (e)
Get key
    • The keys, get the key according to the regular expression
    • Write the following code
From Redis import *if __name__== "__main__":    try:        #创建StrictRedis对象, built with Redis server connection        Sr=strictredis ()        #获取所有的键        Result=sr.keys ()        #输出响应结果, all keys form a list and return an empty list if there is no key        print (result)    except Exception as E:        print (e)

Now you're ready to start using Redis.

Redis interacts 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.