python中使用redis的常用命令

本文涉及的产品
云数据库 Redis 版,标准版 2GB
推荐场景:
搭建游戏排行榜
云原生内存数据库 Tair,内存型 2GB
简介: Python中使用Redis的常用命令。redis-py库提供了一个简单而强大的接口来执行Redis的各种操作。你可以根据需要选择合适的命令来操作Redis数据结构。

python中使用redis的常用命令

在Python中使用Redis,可以通过redis-py库来执行各种Redis命令。以下是一些常用的Redis命令及其在redis-py中的实现示例:

安装:pip install redis或者conda install redis-py, 安装完成后,就可以使用import redis来导入库了。

1. 连接到Redis服务器

import redis

# 创建一个Redis连接
r = redis.Redis(host='localhost', port=6379, password='123456', db=0)

2. 键值操作

设置键值

r.set('key', 'value')

获取键值

value = r.get('key')
print(value)  # 输出: b'value'

删除键

r.delete('key')

检查键是否存在

exists = r.exists('key')
print(exists)  # 输出: 0 或 1

3. 字符串操作

增加数值键

r.incr('counter')

减少数值键

r.decr('counter')

4. 列表操作

左推入

r.lpush('mylist', 'value1')

右推入

r.rpush('mylist', 'value2')

获取列表元素

values = r.lrange('mylist', 0, -1)
print(values)  # 输出: [b'value1', b'value2']

弹出左侧元素

value = r.lpop('mylist')
print(value)  # 输出: b'value1'

弹出右侧元素

value = r.rpop('mylist')
print(value)  # 输出: b'value2'

5. 哈希操作

设置哈希字段

r.hset('myhash', 'field1', 'value1')

获取哈希字段

value = r.hget('myhash', 'field1')
print(value)  # 输出: b'value1'

获取所有哈希字段和值

fields = r.hgetall('myhash')
print(fields)  # 输出: {b'field1': b'value1'}

删除哈希字段

r.hdel('myhash', 'field1')

6. 集合操作

添加成员

r.sadd('myset', 'member1')

获取所有成员

members = r.smembers('myset')
print(members)  # 输出: {b'member1'}

删除成员

r.srem('myset', 'member1')

7. 有序集合操作

添加成员和分数

r.zadd('myzset', {
   'member1': 1.0})

获取成员的分数

score = r.zscore('myzset', 'member1')
print(score)  # 输出: 1.0

获取成员的范围

members = r.zrange('myzset', 0, -1)
print(members)  # 输出: [b'member1']

8. 事务操作

使用事务

with r.pipeline() as pipe:
    pipe.set('key1', 'value1')
    pipe.set('key2', 'value2')
    pipe.execute()

9. 发布/订阅

订阅频道

def message_handler(message):
    print(f"Received message: {message['data']}")

pubsub = r.pubsub()
pubsub.subscribe(**{
   'mychannel': message_handler})
pubsub.run_in_thread(sleep_time=0.001)

发布消息

r.publish('mychannel', 'Hello, World!')

10. 键过期

设置键过期时间

r.setex('key', 60, 'value')  # 60秒后过期

获取剩余过期时间

ttl = r.ttl('key')
print(ttl)  # 输出: 剩余时间(秒)

总结

这些是一些在Python中使用Redis的常用命令。redis-py库提供了一个简单而强大的接口来执行Redis的各种操作。你可以根据需要选择合适的命令来操作Redis数据结构。

#

【转载自:】开思通智网 ---- “一起来O站,玩转AGI!”
【官网:】https://www.opensnn.com/
【原文链接:】https://www.opensnn.com/os/article/10000794

结束
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
26天前
|
NoSQL Unix 网络安全
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
|
6天前
|
Unix Shell Linux
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
9 1
|
26天前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
|
1月前
|
存储 消息中间件 NoSQL
Redis命令详解以及存储原理
Redis命令详解以及存储原理
|
23天前
|
Linux Shell 数据库
python Django教程 之 安装、基本命令、视图与网站
python Django教程 之 安装、基本命令、视图与网站
|
26天前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"
【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"
|
27天前
|
SQL 缓存 NoSQL
【Azure Redis 缓存】使用Azure Redis服务时候,如突然遇见异常,遇见命令Timeout performing SET xxxxxx等情况,如何第一时间查看是否有Failover存在呢?
【Azure Redis 缓存】使用Azure Redis服务时候,如突然遇见异常,遇见命令Timeout performing SET xxxxxx等情况,如何第一时间查看是否有Failover存在呢?
|
27天前
|
缓存 监控 NoSQL
【Azure Redis 缓存】Azure Redis出现了超时问题后,记录一步一步的排查出异常的客户端连接和所执行命令的步骤
【Azure Redis 缓存】Azure Redis出现了超时问题后,记录一步一步的排查出异常的客户端连接和所执行命令的步骤
|
27天前
|
缓存 监控 NoSQL
【Azure Redis 缓存】使用Python代码获取Azure Redis的监控指标值 (含Powershell脚本方式)
【Azure Redis 缓存】使用Python代码获取Azure Redis的监控指标值 (含Powershell脚本方式)
|
28天前
|
NoSQL 网络安全 Redis
用python安装redis并设置服务自启
用python安装redis并设置服务自启
14 0