Redis interacts with Python

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

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 Strictredis

    • This module provides StrictRedis对象 , for connection to Redis servers, and provides different methods for different types of operations, in-line interaction

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)简写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对象,与redis服务器建?连接 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对象,与redis服务器建?连接 sr=StrictRedis() #添加键name,值为itheima result=sr.set(‘name‘,‘itheima‘) #输出响应结果,如果添加成功则返回True,否则返回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对象,与redis服务器建?连接 sr=StrictRedis() #获取键name的值 result = sr.get(‘name‘) #输出键的值,如果键不存在则返回None 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对象,与redis服务器建?连接 sr=StrictRedis() #设置键name的值,如果键已经存在则进?修改,如果键不存在则进?添加 result = sr.set(‘name‘,‘itcast‘) #输出响应结果,如果操作成功则返回True,否则返回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对象,与redis服务器建?连接 sr=StrictRedis() #设置键name的值,如果键已经存在则进?修改,如果键不存在则进?添加 result = sr.delete(‘name‘) #输出响应结果,如果删除成功则返回受影响的键数,否则则返回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对象,与redis服务器建?连接 sr=StrictRedis() #获取所有的键 result=sr.keys() #输出响应结果,所有的键构成?个列表,如果没有键则返回空列表 print(result) except Exception as e: print(e)

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.