Python对Redis基础操作

安装redis-py库

pip install redis

连接到Redis、进行读写操作并关闭连接:

import redis

def connect_to_redis(host, port, password):
    try:
        # 创建Redis连接对象
        redis_client = redis.StrictRedis(
            host=host,
            port=port,
            password=password,
            decode_responses=True
        )
        # 测试连接
        redis_client.ping()
        print("成功连接到Redis")
        return redis_client
    except Exception as e:
        print(f"连接Redis失败: {e}")
        return None

def write_to_redis(redis_client, key, value):
    try:
        # 将数据写入Redis
        redis_client.set(key, value)
        print(f"成功写入数据: {key} = {value}")
    except Exception as e:
        print(f"写入数据失败: {e}")

def read_from_redis(redis_client, key):
    try:
        # 从Redis中读取数据
        value = redis_client.get(key)
        if value:
            print(f"读取的值: {value}")
        else:
            print(f"Key '{key}' 不存在")
    except Exception as e:
        print(f"读取数据失败: {e}")

def close_redis_connection(redis_client):
    # Redis-py使用连接池,因此无需显式关闭连接
    # 但是可以关闭连接池中的所有连接
    try:
        redis_client.connection_pool.disconnect()
        print("成功关闭Redis连接")
    except Exception as e:
        print(f"关闭Redis连接失败: {e}")

if __name__ == "__main__":
    host = "127.0.0.1"
    port = 8899
    password = "123456"
    key_to_write = "key1"
    value_to_write = "value1"
    key_to_read = "key1"

    # 连接到Redis
    redis_client = connect_to_redis(host, port, password)

    if redis_client:
        # 向Redis中写入数据
        write_to_redis(redis_client, key_to_write, value_to_write)

        # 从Redis中读取数据
        read_from_redis(redis_client, key_to_read)

        # 关闭Redis连接
        close_redis_connection(redis_client)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值