ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

python操作redis教程

2022-02-10 11:59:19  阅读:177  来源: 互联网

标签:教程 python redis print host test port pool


1. python中安装redis模块包

pip install redis

2. python连接redis

redis 提供两个类 Redis 和 StrictRedis, StrictRedis 用于实现大部分官方的命令,Redis 是 StrictRedis 的子类,用于向后兼用旧版本。
redis 取出的结果默认是字节,我们可以设定 decode_responses=True 改成字符串。

import redis

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

r = redis.StrictRedis(host=host,port=port,db=0)
key = r.keys()
print(key)

r = redis.StrictRedis(host=host,port=port,db=0,decode_responses=True)
key = r.keys()
print(key)

在这里插入图片描述

连接池

redis-py 使用 connection pool 来管理对一个 redis server 的所有连接,避免每次建立、释放连接的开销。

默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数 Redis,这样就可以实现多个 Redis 实例共享一个连接池。

import redis

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

key = r.keys()
print(key)

3. redis基本命令 - string

3.1 set(name, value, ex=None, px=None, nx=False, xx=False)

在 Redis 中设置值,默认,不存在则创建,存在则修改。

参数:

  • ex - 过期时间(秒)
  • px - 过期时间(毫秒)
  • nx - 如果设置为True,则只有name不存在时,当前set操作才执行
  • xx - 如果设置为True,则只有name存在时,当前set操作才执行

ex - 过期时间(秒) 这里过期时间是3秒,3秒后,键 test 的值就变成None

import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

r.set('test','123456',ex=3)  # 设置键 test的值为123456,过期时间为3秒
print("test的值为:{}".format(r.get('test')))

time.sleep(3)
print("3秒后test的值为:{}".format(r.get('test')))

在这里插入图片描述
px - 过期时间(豪秒) 这里过期时间是3豪秒,3毫秒后,键foo的值就变成None

import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

r.set('test','1234',px=3)  # 设置键 test 的值为1234,过期时间为3毫秒
print("test的值为:{}".format(r.get('test')))

time.sleep(0.003)
print("3毫秒后test的值为:{}".format(r.get('test')))

在这里插入图片描述
nx - 如果设置为True,则只有name不存在时,当前set操作才执行 (新建)

import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("当前数据库中存在的键有:{}".format(r.keys()))
print("若test键不存在,则设置test的值为:123456,否则不执行")
r.set('test','123456',nx=True)  # 当test键不存在时,设置键 test 的值为123456
print("test的值为:{}".format(r.get('test')))

在这里插入图片描述
xx - 如果设置为True,则只有name存在时,当前set操作才执行 (修改)

import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("当前数据库中存在的键有:{}".format(r.keys()))
print("若test键存在,则修改test的值为:123456,否则不执行")
r.set('test','123456',xx=True)  # 当test键存在时,修改键 test 的值为123456
print("test的值为:{}".format(r.get('test')))

在这里插入图片描述

3.2 setnx(name, value)

设置值,只有name不存在时,执行设置操作(添加)

3.3 setex(name, time, value)

设置值,time - 过期时间(数字秒 或 timedelta对象)

3.4 psetex(name, time_ms, value)

设置值,time_ms - 过期时间(数字毫秒 或 timedelta对象)

3.5 mset 和 mget

  • mset(*args, **kwargs):批量设置值
  • mget(keys, *args):批量获取值
import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("当前数据库中存在的键有:{}".format(r.keys()))

print("设置键 k1和k2的值,分别为v1和v2")
r.mset({'k1': 'v1', 'k2': 'v2'})
print("获取键k1和k2的值")
print(r.mget("k1", "k2"))   # 一次取出多个键对应的值

在这里插入图片描述

3.6 getset(name, value)

设置新值并获取原来的值

import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("当前数据库中存在的键有:{}".format(r.keys()))

print("获取键test的值为:{}".format(r.get('test')))

result = r.getset('test','12345')  # 设置键test的新值为12345,并返回原test的值1234
print("键test的原值为:{}".format(result))
print("获取键test的当前的值为:{}".format(r.get('test')))

在这里插入图片描述

3.7 getrange(key, start, end)

获取子序列(根据索引、字节获取)一个汉字占3个字节

参数:

  • start - 起始位置(起始索引从0开始)
  • end - 结束位置
import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("当前数据库中存在的键有:{}".format(r.keys()))
print("获取键test的值为:{}".format(r.get('test')))

result = r.getrange('test',0,1)  # 获取索引0到1的子序列
print("键test的0到1的子序列为:{}".format(result))

result = r.getrange('test',1,4)  # 获取索引1到4的子序列
print("键test的1到4的子序列为:{}".format(result))

在这里插入图片描述

import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("当前数据库中存在的键有:{}".format(r.keys()))
print("获取键name的值为:{}".format(r.get('name')))

result = r.getrange('name',0,2)  # 获取索引0到2的子序列
print("键name的0到2的子序列为:{}".format(result))

result = r.getrange('name',3,20)  # 获取索引3到20的子序列
print("键name的3到20的子序列为:{}".format(result))

在这里插入图片描述
注意:中文一个汉字占3个字节,如果取0到1的子序列,则会报错(不到一个汉字)

3.8 setrange(name, offset, value)

修改字符串内容,从指定字符串索引开始向后替换(新值太长时,则向后添加)

参数:

  • offset - 字符串的索引,字节(一个汉字三个字节)
  • value - 要设置的值
import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("当前数据库中存在的键有:{}".format(r.keys()))
print("获取键name的值为:{}".format(r.get('name')))

print('将name的第一个字节开始的值改为“我”,占3个字节')
r.setrange('name',0,'我')  # 
print("键name的值修改为:{}".format(r.get('name')))

在这里插入图片描述

3.9 strlen(name)

返回name对应值的字节长度(一个汉字3个字节)

import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("获取键test的值为:{}".format(r.get('test')))
result = r.strlen('test')
print("键test的字节长度为:{}".format(result))

print("获取键name的值为:{}".format(r.get('name')))
result = r.strlen('name')
print("键name的字节长度为:{}".format(result))

在这里插入图片描述

3.10 incr(key, amount=1)

自增 key对应的值,当 key不存在时,则创建 key=amount,否则,则自增。

参数:amount - 自增数(必须是整数)

import redis
import time

host = '192.168.149.153' # redis服务地址
port = 6379  # redis服务端口

pool = redis.ConnectionPool(host=host,port=port,db=0,decode_responses=True)
r = redis.Redis(connection_pool=pool) # 或者用redis.StrictRedis

print("获取键num的值为:{}".format(r.get('num')))
print("获取键num1的值为:{}".format(r.get('num1')))

print("将键num的值增1")
r.incr('num',amount=1)
print("获取键num当前的值为:{}".format(r.get('num')))

r.incr('num1',amount=10)
print("获取键num1当前的值为:{}".format(r.get('num1')))

在这里插入图片描述

3.11 decr(key, amount=1)

自减 key对应的值,当 key不存在时,则创建 name=amount,否则,则自减。

参数:amount - 自减数(整数)

3.12 append(key, value)

在key对应的值后面追加内容

参数:value - 要追加的字符串

4. redis 基本命令 list

标签:教程,python,redis,print,host,test,port,pool
来源: https://blog.csdn.net/d1240673769/article/details/122854057

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有