展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

python操作redis

  • 使用docker安装部署
[root@node1 ~]#  docker pull redis:6.2.10
6.2.10: Pulling from library/redis
bb263680fed1: Pull complete 
ac509f65c3e9: Pull complete 
51afc2cce3df: Pull complete 
45afedfc27f7: Pull complete 
3aa14ffa5593: Pull complete 
2fff93ae5a36: Pull complete 
Digest: sha256:5cdb2ac6f780e1d250787f4a887c1ed1166e3389757189ea913059409acc6f7c
Status: Downloaded newer image for redis:6.2.10
[root@node1 ~]# docker run --name redis -p 6379:6379 -v /docker/redis/data:/data  -v /docker/redis/conf/redis.conf:/etc/redis/redis.conf -d redis:6.2.10 redis-server /etc/redis/redis.conf
feb99954b555cba9d7268374f877a5d282cfaf6237c7fee5b846f1d15ab65384
[root@node1 ~]# docker start redis
redis
[root@node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
feb99954b555        redis:6.2.10        "docker-entrypoint.s…"   10 seconds ago      Up 9 seconds        0.0.0.0:6379->6379/tcp   redis
[root@node1 ~]# docker exec -it redis bash
root@feb99954b555:/data# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
(empty array)
  • 安装依赖
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple redis
  • 案例1
import redis
import time
import random

# 创建一个Redis连接
redis_client = redis.Redis(host='192.168.128.100', port=6379, db=0)

# 循环写入10个值到Redis
for i in range(10):
    key = f'key_{i}'
    value = f'value_{i}'
    redis_client.set(key, value)
    print(f"Written to Redis: {key} - {value}")

# 打印所有写入的值
for i in range(10):
    key = f'key_{i}'
    retrieved_value = redis_client.get(key)
    print(f"Retrieved value from Redis: {key} - {retrieved_value.decode('utf-8')}")
  • 控制台打印
Written to Redis: key_0 - value_0
Written to Redis: key_1 - value_1
Written to Redis: key_2 - value_2
Written to Redis: key_3 - value_3
Written to Redis: key_4 - value_4
Written to Redis: key_5 - value_5
Written to Redis: key_6 - value_6
Written to Redis: key_7 - value_7
Written to Redis: key_8 - value_8
Written to Redis: key_9 - value_9
Retrieved value from Redis: key_0 - value_0
Retrieved value from Redis: key_1 - value_1
Retrieved value from Redis: key_2 - value_2
Retrieved value from Redis: key_3 - value_3
Retrieved value from Redis: key_4 - value_4
Retrieved value from Redis: key_5 - value_5
Retrieved value from Redis: key_6 - value_6
Retrieved value from Redis: key_7 - value_7
Retrieved value from Redis: key_8 - value_8
Retrieved value from Redis: key_9 - value_9
  • 案例2
import redis

r = redis.Redis(host='192.168.128.100', port=6379, db=0)
# Example with lists
list_key = 'my_list'

# Push items to the list
r.rpush(list_key, 'item1')
r.rpush(list_key, 'item2')
r.rpush(list_key, 'item3')

# Retrieve items from the list
list_values = r.lrange(list_key, 0, -1)
print("List values:", list_values)

# Example with sets
set_key = 'my_set'

# Add items to the set
r.sadd(set_key, 'itemA')
r.sadd(set_key, 'itemB')
r.sadd(set_key, 'itemC')

# Retrieve items from the set
set_values = r.smembers(set_key)
print("Set values:", set_values)
  • 控制台打印
List values: [b'item1', b'item2', b'item3', b'item1', b'item2', b'item3']
Set values: {b'itemA', b'itemC', b'itemB'}
posted @ 2024-01-17 09:21  DogLeftover  阅读(6)  评论(0编辑  收藏  举报