Home > Database > Redis > body text

A brief discussion on how to install and use the redis module in Python

青灯夜游
Release: 2021-08-02 10:09:21
forward
3007 people have browsed it

This article will introduce you to how to install and use the redis module in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on how to install and use the redis module in Python

##[Related recommendations:

Redis video tutorial]

Installation and use

Installation

Install redis module

pip3 install redis
Copy after login

Normal connection

redis-py provides two classes Redis and StrictRedis for implementation Redis commands, StrictRedis is used to implement most official commands and use official syntax and commands. Redis is a subclass of StrictRedis and is used for backward compatibility with older versions of redis-py

import redis
conn = redis.Redis(host='127.0.0.1', port=6379)
# 可以使用url方式连接到数据库
# conn = Redis.from_url('redis://@localhost:6379/1')
conn.set('name', 'LinWOW')
print(conn.get('name'))
Copy after login

Connection Pool

redis-py uses connection pool to manage all connections to a redis server to avoid the overhead of establishing and releasing connections each time. By default, each Redis instance maintains its own connection pool. You can directly create a connection pool and then use it as parameter Redis, so that multiple Redis instances can share a connection pool

Connection pool: redis_pool.py

from redis import ConnectionPool
POOL=ConnectionPool(host='127.0.0.1',port=6379,max_connections=100)
Copy after login

Use connection pool: test_redis.py

import redis
from redis_pool import POOl
conn = redis.Redis(connection_pool=POOl)
conn.set('name', 'LinWOW')
print(conn.get('name'))
Copy after login

Construct the url method to connect to the database, there are the following three modes:

redis://[:password]@host:port/db    # TCP连接
rediss://[:password]@host:port/db   # Redis TCP+SSL 连接
unix://[:password]@/path/to/socket.sock?db=db    # Redis Unix Socket 连接
Copy after login

Python operation Redis

String operation

MethodFunctionExampleExample resultset(name, value, ex=None, px=None, nx=False, xx=False)ex, expiration time (s); px, expiration time (ms); nx, if set to True, the current set operation will be executed only when name does not exist. If the value exists, it cannot be modified and the execution will have no effect; xx, if set to True, the current set operation will be executed only if name exists. The value can be modified only if the value exists. If the value does not exist, the new value will not be set. The effect is the same as setex and setnxset(name , value)Assign value to nameredis.set('name', 'Bob')Trueget(name)Returns the value of the string whose key is name in the databaseredis.get('name')b'Bob'getset(name, value)Assign value to the string whose key is name in the database and return the last valueredis.getset('name', 'Mike')b'Bob'##mget(keys, *args)setnx(name, value )setex(name, time, value)setrange(name, offset, value)mset(mapping)msetnx(mapping)incr(name, amount=1)decr(name, amount=1)append(key, value)substr(name, start, end=-1)getrange(key, start, end)

Key operation


Return the value corresponding to multiple keysredis.mget(['name', 'nickname'])[b'Mike', b'Miker']
Set the value if the key does not existredis.setnx('newname', 'James')True for the first time, False for the second time
Set the corresponding value to a value of string type, and specify the validity period corresponding to this key valueredis .setex('name', 1, 'James')True
Set the specified key Substring of value valueredis.set('name', 'Hello') redis.setrange('name', 6, 'World')11, modified characters String length
Batch assignmentredis.mset({'name1': 'Durant', 'name2': ' James'})True
Batch assignment only when none of the keys existredis.msetnx ({'name3': 'Smith', 'name4': 'Curry'})True
key is the value-added operation of name. The default is 1. If the key does not exist, it will be created and set to amountredis.incr('age', 1)1, that is, after modification The value of
key is the value decrement operation of name. The default is 1. If the key does not exist, it is created and set to - amountredis.decr('age', 1)-1, which is the modified value
key appends value to the string value of nameredis.append('nickname', 'OK')13, which is the modified string length
Returns the substring of the value of the string whose key is nameredis.substr('name' , 1, 4)b'ello'
Get the value of key from start to end Substring of redis.getrange('name', 1, 4)b'ello'
##move(name, db)Move key to other databasemove('name ', 2)Trueflushdb()Delete all keys in the currently selected databaseflushdb()Trueflushall()Delete all keys in all databasesflushall()True
MethodFunctionExampleExample result
exists(name)Determine whether a key existsredis.exists('name')True
delete(name)Delete a keyredis.delete('name')1
type(name)Judge the key typeredis.type('name')b'string'
keys(pattern)Get all keys that match the rulesredis.keys('n*')[b' name']
randomkey()Get a random keyrandomkey()b'name'
rename(src, dst)Rename the keyredis.rename('name', 'nickname') True
dbsize()Get the number of keys in the current databasedbsize()100
expire(name, time)Set the expiration time of the key in secondsredis.expire('name', 2) True
ttl(name)Get the expiration time of the key in seconds, -1 means it will never expireredis.ttl('name ')-1

List operation

##Methodrpush(name, *values)lpush(name, *values)llen(name)##lrange(name, start, end)Returns the elements between start and end in the list whose key is name redis.lrange('list', 1, 3)[b'3', b'2', b'1']ltrim(name, start, end)Intercept the list whose key is name and retain the content with index from start to endltrim('list', 1, 3)Truelindex(name, index)Returns the element at index position in the list with key nameredis.lindex(' list', 1)b'2'lset(name, index, value)Give the index position in the list with key name If the element is assigned a value, an error will be reported if it crosses the boundary. redis.lset('list', 1, 5)Truelrem(name, count, value)Delete the elements whose value is value in the list of count keysredis.lrem('list', 2, 3)1, that is, the deleted elements Numberlpop(name)Return and delete the first element in the list with key nameredis.lpop('list')b'5'rpop(name)Return and delete the last element in the list with key nameredis .rpop('list')b'2'blpop(keys, timeout=0)Return and delete the name in keys The first element in the list, if the list is empty, it will always block and waitredis.blpop('list')[b'5']brpop(keys, timeout=0)Return and delete the last element in the list whose key is name. If the list is empty, it will always block and waitredis .brpop('list')[b'2']rpoplpush(src, dst)Return and delete the file named src The tail element of the list and adds the element to the head of the list named dstredis.rpoplpush('list', 'list2')b'2'

应用场景:

blpop实现简单分布式爬虫:

多个url放到列表里,往里不停放URL,程序循环取值,但是只能一台机器运行取值,可以把url放到redis中,多台机器从redis中取值,爬取数据,实现简单分布式

将多个列表排列,按照从左到右去pop对应列表的元素
参数:
keys,redis的name的集合
timeout,超时时间,当元素所有列表的元素获取完之后,阻塞等待列表内有数据的时间(秒), 0 表示永远阻塞
更多:
r.brpop(keys, timeout),从右向左获取数据

自定义增量迭代:

由于redis类库中没有提供对列表元素的增量迭代,如果想要循环name对应的列表的所有元素,那么就需要:

1、获取name对应的所有列表

2、循环列表

但是,如果列表非常大,那么就有可能在第一步时就将程序的内容撑爆,所有有必要自定义一个增量迭代的功能:

import redis
conn=redis.Redis(host='127.0.0.1',port=6379)
# conn.lpush('test',*[1,2,3,4,45,5,6,7,7,8,43,5,6,768,89,9,65,4,23,54,6757,8,68])
# conn.flushall()
def scan_list(name,count=2):
    index=0
    while True:
        data_list=conn.lrange(name,index,count+index-1)
        if not data_list:
            return
        index+=count
        for item in data_list:
            yield item
print(conn.lrange('test',0,100))
for item in scan_list('test',5):
    print('---')
    print(item)
Copy after login

Set操作

FunctionExampleExample result
Add at the end of the list with key name For elements whose value is value, you can pass multiple redis.rpush('list', 1, 2, 3)3, list size
Add an element with value as value in the list header with key name, you can pass multiple redis.lpush('list', 0)4, list size
Returns the length of the list with key nameredis.llen(' list')4
方法作用示例示例结果
sadd(name, *values)向key为name的set中添加元素redis.sadd(‘tags’, ‘Book’, ‘Tea’, ‘Coffee’)3,即插入的数据个数
srem(name, *values)从key为name的set中删除元素redis.srem(‘tags’, ‘Book’)1,即删除的数据个数
spop(name)随机返回并删除key为name的set中一个元素redis.spop(‘tags’)b’Tea’
smove(src, dst, value)从src对应的set中移除元素并添加到dst对应的set中redis.smove(‘tags’, ‘tags2’, ‘Coffee’)True
scard(name)返回key为name的set的元素个数redis.scard(‘tags’)3
sismember(name, value)测试member是否是key为name的set的元素redis.sismember(‘tags’, ‘Book’)True
sinter(keys, *args)返回所有给定key的set的交集redis.sinter([‘tags’, ‘tags2’]){b’Coffee’}
sinterstore(dest, keys, *args)求交集并将交集保存到dest的集合redis.sinterstore(‘inttag’, [‘tags’, ‘tags2’])1
sunion(keys, *args)返回所有给定key的set的并集redis.sunion([‘tags’, ‘tags2’]){b’Coffee’, b’Book’, b’Pen’}
sunionstore(dest, keys, *args)求并集并将并集保存到dest的集合redis.sunionstore(‘inttag’, [‘tags’, ‘tags2’])3
sdiff(keys, *args)返回所有给定key的set的差集redis.sdiff([‘tags’, ‘tags2’]){b’Book’, b’Pen’}
sdiffstore(dest, keys, *args)求差集并将差集保存到dest的集合redis.sdiffstore(‘inttag’, [‘tags’, ‘tags2’])3
smembers(name)返回key为name的set的所有元素redis.smembers(‘tags’){b’Pen’, b’Book’, b’Coffee’}
srandmember(name)随机返回key为name的set的一个元素,但不删除元素redis.srandmember(‘tags’)

Sorted Set operation

MethodFunctionExampleExample result
zadd(name, args, *kwargs)Add element member to the zset whose key is name, and score is used for sorting. If the element exists, update its orderredis.zadd('grade', 100, 'Bob', 98, 'Mike')2, which is the number of added elements
zrem(name, *values)Delete the elements in the zset whose key is nameredis.zrem('grade', 'Mike' )1, that is, the number of deleted elements
zincrby(name, value, amount=1)If in the zset whose key is name If the element value already exists in the collection, the score of the element is increased by amount. Otherwise, the element is added to the collection and its score value is amountredis.zincrby('grade', 'Bob', -2) 98.0, that is, the modified value
zrank(name, value)Returns the ranking of the elements in the zset whose key is name (by score Sort from small to large) that is, subscript redis.zrank('grade', 'Amy')1
zrevrank(name, value )Returns the reciprocal ranking of the elements in the zset with key name (sorted from large to small by score), that is, the subscript redis.zrevrank('grade', 'Amy')2
zrevrange(name, start, end, withscores=False)Returns the zset whose key is name (sorted by score from large to small) All elements with index from start to endredis.zrevrange('grade', 0, 3)[b'Bob', b'Mike', b'Amy', b'James']
zrangebyscore(name, min, max, start=None, num=None, withscores=False)Return to the zset with key name Score elements in the given intervalredis.zrangebyscore('grade', 80, 95)[b'Amy', b'James']
zcount(name, min, max)Returns the number of scores in the given interval in the zset whose key is nameredis.zcount('grade', 80, 95) 2
zcard(name)Returns the number of elements of the zset whose key is nameredis.zcard(' grade')3
zremrangebyrank(name, min, max)Delete the elements ranked in the given interval in the zset with key name redis.zremrangebyrank('grade', 0, 0)1, which is the number of deleted elements
zremrangebyscore(name, min, max)Delete the elements whose score is in the given interval in the zset with key nameredis.zremrangebyscore('grade', 80, 90)1, that is, delete Number of elements

Hash operation

##hget(name, key)Returns the value corresponding to the field in the hash whose key is nameredis.hget('price', 'cake')5##hmget(name, keys, *args)hmset(name, mapping)##hincrby(name, key, amount=1)will The key is the value mapped in the hash of name. Add amountredis.hincrby('price', 'apple', 3)6 to the modified valuehexists(name, key)key is whether there is a mapping with the key named key in namehashredis.hexists('price', 'banana')Truehdel(name, *keys)key is the mapping that deletes the key named key in namehashredis.hdel( 'price', 'banana')Truehlen(name)Get the number of mappings from the hash with key nameredis.hlen('price')6hkeys(name)Get all the hashes with key name Mapping key nameredis.hkeys('price')[b'cake', b'book', b'banana', b'pear']hvals(name)Get all mapping key values ​​from the hash with key nameredis.hvals('price')[b '5', b'6', b'2', b'6']hgetall(name)Get all the hashes with key name Mapping key-value pairsredis.hgetall('price'){b'cake': b'5', b'book': b'6', b'orange': b'7', b'pear': b'6'}

注意点:

hscan(name, cursor=0, match=None, count=None):增量式迭代获取,对于数据大的数据非常有用,hscan可以实现分片的获取数据,并非一次性将数据全部获取完,从而放置内存被撑爆

参数:
    name,redis的name
    cursor,游标(基于游标分批取获取数据)
    match,匹配指定key,默认None 表示所有的key
    count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
如:
    第一次:cursor1, data1 = r.hscan('xx', cursor=0, match=None, count=None)
    第二次:cursor2, data1 = r.hscan('xx', cursor=cursor1, match=None, count=None)
    ...
    直到返回值cursor的值为0时,表示数据已经通过分片获取完毕
Copy after login

hscan_iter(name, match=None, count=None): 利用yield封装hscan创建生成器,实现分批去redis中获取数据

参数:
    match,匹配指定key,默认None 表示所有的key
    count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
如:    for item in r.hscan_iter('xx'):
        print item
Copy after login

管道

redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。

import redis
pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
r = redis.Redis(connection_pool=pool)
# pipe = r.pipeline(transaction=False)
pipe = r.pipeline(transaction=True)
pipe.multi()
pipe.set('name', 'linwow')
pipe.set('age', '18')
pipe.execute()
Copy after login

Django中使用redis

方式一:

utils文件夹下,建立redis_pool.py

import redis
POOL = redis.ConnectionPool(host='127.0.0.1', port=6379,password='1234',max_connections=1000)
Copy after login

视图函数中使用:

import redis
from django.shortcuts import render,HttpResponse
from redis_pool import POOL

def index(request):
    conn = redis.Redis(connection_pool=POOL)
    conn.hset('liwow','age',18)
    return HttpResponse('设置成功')
    
def order(request):
    conn = redis.Redis(connection_pool=POOL)
    conn.hget('kkk','age')
    return HttpResponse('获取成功')
Copy after login

方式二:

安装django-redis模块

pip3 install django-redis
Copy after login

setting里配置:

# redis配置
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections": 100}
            # "PASSWORD": "123",
        }
    }
}
Copy after login

视图函数:

from django_redis import get_redis_connection
conn = get_redis_connection('default')
print(conn.hgetall('xxx'))
Copy after login

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of A brief discussion on how to install and use the redis module in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MethodFunction ExampleExample result
hset(name, key, value)The key is Add mapping to the hash of name hset('price', 'cake', 5)1, which is the number of added mappings
hsetnx(name, key, value)Add mapping to the hash with key name, if the mapping key name does not existhsetnx('price', 'book', 6)1, that is, the number of added mappings
Return The key is the value corresponding to each key in the hash of nameredis.hmget('price', ['apple', 'orange'])[b'3', b'7 ']
Add mappings in batches to the hash with key nameredis.hmset('price', {'banana': 2, 'pear': 6})True