python操作redis

首先我们先了解python 集成redis有哪些优势

  1. 性能:Redis 是一个基于内存的数据结构存储系统,相较于传统的关系型数据库,它具有更高的读写性能。

  2. 数据类型:Redis 支持丰富的数据类型,如字符串、列表、集合、有序集合、哈希表等,方便存储不同类型的数据。

  3. 功能丰富:Redis 提供了诸如发布/订阅、Lua 脚本、事务、持久化等功能,可以满足各种应用场景的需求。

  4. 易于使用:Python 的 Redis 库(如 redis-py)提供了友好的接口,使得在 Python 中整合 Redis 非常简单。

以下是使用 redis-py 库连接 Redis 并进行简单操作的示例代码:

import redis
 
# 连接到本地Redis实例
redis_client = redis.StrictRedis(host='localhost', port=6379, db=0)
 
# 设置键值对
redis_client.set('key', 'value')
 
# 获取键对应的值
value = redis_client.get('key')
print(value)  # 输出 b'value',因为Redis返回的是字节字符串
 
# 列表操作
redis_client.rpush('mylist', 'element1')
redis_client.rpush('mylist', 'element2')
elements = redis_client.lrange('mylist', 0, -1)
print(elements)  # 输出 b'element1', b'element2'
 
# 关闭连接
redis_client.close()

下面提供一个完整的python操作redis示例,供大家参考:

import redis
import time


class TestRedis:
    def __init__(self):
        self.dbconn = None

    def openRedis(self):
        # 连接redis,加上decode_responses=True,写入的键值对中的value为str类型,不加这个参数写入的则为字节类型。
        # r = redis.Redis(host=u'127.0.0.1', port=6379, db=0)
        pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0, decode_responses=True)  # 使用连接池
        self.dbconn = redis.Redis(connection_pool=pool)

    def addKey(self, Key, Value):
        self.dbconn.set(Key, Value)

    def getKey(self, Key):
        return self.dbconn.get(Key)

    def setObj(self, name, value, ex=None, px=None, nx=False, xx=False):
        #   ex,过期时间(秒)
        #   px,过期时间(毫秒)
        #   nx,如果设置为True,则只有name不存在时,当前set操作才执行
        #   xx,如果设置为True,则只有name存在时,当前set操作才执行
        self.dbconn.set(name, value, ex=ex, px=px, nx=nx, xx=xx)
        pass

    def exAndpxDemo(self):
        # 演示过期时间, ex :秒 , px : 毫秒
        self.setObj("apple", "一个", ex=2)
        print(self.getKey("apple"))
        time.sleep(3)
        print(self.getKey("apple"))
        print("======================")
        self.setObj("banana", "two", px=2)
        print(self.getKey("banana"))
        time.sleep(0.3)
        print(self.getKey("banana"))

    def nxAandxxDemo(self):
        #   nx,如果设置为True,则只有name不存在时,当前set操作才执行 (新建)
        #   xx,如果设置为True,则只有name存在时,当前set操作才执行 (修改)
        self.addKey('grape', 'zhangsan')
        self.setObj("grape", "一个", nx=True)
        self.setObj("grape2", "一个2", nx=True)
        print(self.getKey("grape"))  # 此时是 zhangsan 并没有改为 一个
        print(self.getKey("grape2"))  # 此时是 一个2
        print("========================")
        self.setObj("grape2", "修改了", xx=True)  # grape2已经存在
        print(self.getKey("grape2"))  # 此时是 修改了
        self.setObj("grape3", "修改了", xx=True)  # grape3不存在 不会创建
        print(self.getKey("grape3"))  # None

    def listGet(self):
        # 将目前redis缓存中的键对应的值批量取出来
        print(self.dbconn.mget("grape", 'grape2'))
        print(self.dbconn.mget(['grape', 'grape2']))

    def getset(self):
        # 设置新值并获取原来的值
        print(self.dbconn.getset("grape", "aaa"))
        print(self.dbconn.getset("grape", "bbb"))

    def listSet(self):
        self.dbconn.mget({'k1': 'v1', 'k2': 'v2'})
        self.dbconn.mset(k1="v1", k2="v2")  # 这里k1 和k2 不能带引号 一次设置对个键值对

        opt = {
            "one": "aaa",
            "two": "bbb",
            "three": "ccc",
            4: 4
        }
        self.dbconn.mset(opt)

        print(self.dbconn.mget("k1", "k2"))  # 一次取出多个键对应的值
        print(self.dbconn.mget("one", "two", "three", 4))

    def addClick(self):
        self.dbconn.set("foo", 123)
        print(self.dbconn.mget("foo", "foo1", "foo2", "k1", "k2"))
        self.dbconn.incr("foo")  # 124
        self.dbconn.incr("foo")  # 125
        self.dbconn.decr("foo")  # 自减 124

        print(self.dbconn.mget("foo", "foo1", "foo2", "k1", "k2"))
        self.dbconn.set("foo1", "123.0")

        # 浮点型
        self.dbconn.set("foo1", "123.0")
        self.dbconn.set("foo2", "221.0")
        print(self.dbconn.mget("foo1", "foo2"))
        self.dbconn.incrbyfloat("foo1", amount=2.0)
        self.dbconn.incrbyfloat("foo2", amount=3.0)
        print(self.dbconn.mget("foo1", "foo2"))

    def hash(self):
        opt = {
            "sasa": {
                "v1": "1a",
                "v2": "2a"
            }
        }
        opts = {
            "v1": "1a",
            "v2": "2a"
        }

        self.dbconn.hset("hash1", "k2", "v2")
        self.dbconn.hmset("hash1", opt)
        self.dbconn.hmset("hash1", opts)

        # print(self.dbconn.hkeys("hash1"))                 # 取hash中所有的key
        # print(self.dbconn.hget("hash1", "k1"))            # 单个取hash的key对应的值
        # print(self.dbconn.hmget("hash1", "k1", "v3"))     # 多个取hash的key对应的值

        self.dbconn.hsetnx("hash1", "k3", "v3")    # 只能新建
        # print(self.dbconn.hkeys("hash1"))         # 取hash中所有的key

        print(self.dbconn.hget("hash1","k2"))            # 单个取出"hash2"的key-k2对应的value
        print(self.dbconn.hmget("hash1", "k2", "k3"))   # 批量取出"hash2"的key-k2 k3对应的value --方式1
        print(self.dbconn.hmget("hash1", ["k2", "k3"])) # 批量取出"hash2"的key-k2 k3对应的value --方式2

        print(self.dbconn.hgetall("hash1"))  #   获取name对应hash的所有键值
        print(self.dbconn.hlen("hash1"))     #   获取name对应的hash中键值对的个数
        print(self.dbconn.hkeys("hash1"))    #   获取name对应的hash中所有的key的值
        print(self.dbconn.hvals("hash1"))    #   获取name对应的hash中所有的value的值

        #检查name对应的hash是否存在当前传入的key
        print(self.dbconn.hexists("hash1", "k4"))  # False 不存在
        print(self.dbconn.hexists("hash1", "k1"))  # True 存在

        #删除键值对
        print(self.dbconn.hgetall("hash1"))
        self.dbconn.hset("hash1", "k1","v222")  # 修改已有的key k2
        self.dbconn.hset("hash1", "k11", "v1")  # 新增键值对 k11
        self.dbconn.hdel("hash1", "k11")         # 删除一个键值对
        print(self.dbconn.hgetall("hash1"))

        # 自增自减整数(将key对应的value - -整数
        # 自增1或者2,或者别的整数
        # 负数就是自减)
        # hincrby(name, key, amount=1)
        # 自增name对应的hash中的指定key的值,不存在则创建key = amount
        # 参数:
        # name,redis中的name
        # key, hash对应的key
        # amount,自增数(整数)

        self.dbconn.hset("hash1", "k3", 123)
        self.dbconn.hincrby("hash1", "k3", amount=-1)
        # print(self.dbconn.hgetall("hash1"))
        print(self.dbconn.hget("hash1", "k3"))  # 单个取出"hash2"的key-k3对应的value
        self.dbconn.hincrby("hash1", "k4", amount=1)  # 不存在的话,value默认就是1 print(r.hgetall("hash1"))
        print(self.dbconn.hget("hash1", "k4"))  # 单个取出"hash2"的key-k3对应的value

        #浮点数
        self.dbconn.hset("hash1", "k5", "1.0")
        self.dbconn.hincrbyfloat("hash1", "k5",amount=-1.0)  # 已经存在,递减-1.0
        self.dbconn.hincrbyfloat("hash1", "k6", amount=-1.0) # 不存在,value初始值是-1.0 每次递减1.0

        # 取值查看 - -分片读取
        # 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时,表示数据已经通过分片获取完毕
        print(self.dbconn.hscan("hash1"))

        # hscan_iter(name, match=None, count=None)
        # 利用yield封装hscan创建生成器,实现分批去redis中获取数据
        # 参数:
        # match,匹配指定key,默认None
        # 表示所有的key
        # count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
        # 如:

        for item in self.dbconn.hscan_iter('hash1',match=None, count=None):
            print(item)

        print(self.dbconn.hscan_iter("hash1"))  # 生成器内存地址

    def redisList(self):
        # self.dbconn.lpush("list1", 44, 55, 66)    # 表示从左向右操作
        # print(self.dbconn.lrange('list1', 0, -1)) #切片取出值,范围是索引号0到-1(最后一个元素)

        # self.dbconn.rpush("list2", 44, 55, 66)  # 表示从右向左操作
        # print(self.dbconn.llen("list2"))  # 列表长度
        print(self.dbconn.lrange("list2", 0, -1))  # 切片取出值,范围是索引号0-3

        # 往已经有的name的列表的左边添加元素,没有的话无法创建
        # lpushx(name, value)
        # 在name对应的list中添加元素,只有name已经存在时,值添加到列表的最左边




    def main(self):
        r = self.openRedis()
        # self.dbconn.append("name", "haha")  # 在name对应的值junxi后面追加字符串haha
        # print(self.dbconn.mget("name"))

        self.addKey('grape', 'zhangsans')    # 添加
        # print self.getKey('grape')           # 获取
        # self.exAndpxDemo()
        # self.nxAandxxDemo()
        # self.listGet()
        # self.getset()                      #设置新值并获取原来的值
        # self.listSet()                      #批量设置值
        # self.addClick()                       #自增自减

        # redis基本命令 hash
        # self.hash()

        #redis基本命令 list
        self.redisList()

# 将目前redis缓存中的键对应的值批量取出来

if __name__ == '__main__':

    testredis = TestRedis()
    testredis.main()

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值