Python操作Redis

45 阅读2分钟

redispy安装及连接

pip install redis
Installing collected packages: redis
Successfully installed redis-5.2.0
PS C:\Users\Administrator> pip show redis
Name: redis
Version: 5.2.0
Summary: Python client for Redis database and key-value store
Home-page: https://github.com/redis/redis-py
Author: Redis Inc.
Author-email: oss@redis.com
License: MIT
Location: D:\Development\Anaconda\Lib\site-packages
Requires:
Required-by:

连接

import redis  
  
r = redis.StrictRedis(host='localhost', port=6379, db=0)  
print(r)

图片.png

字符串相关操作

import redis

class TestString(object):
    def __init__(self):
        self.r = redis.StrictRedis(host="127.0.0.1", port=6379)

    def set_string(self, skey, svalue):
        """
        设置字符串(单个)
        :param skey:
        :param svalue:
        :return:
        """
        res = self.r.set(skey, svalue)
        print(res)

    def get_string(self):
        """
        获取字符串
        :return:
        """
        res = self.r.get("user")
        print(res)


    def string_mset(self):
        """
        设置多个字符串
        :return:
        """
        d = {
            'user2':'muyi2',
            'user3':'muyi3'
        }
        res = self.r.mset(d)


    def string_mget(self):
        l = ['user2','user3']
        res = self.r.mget(l)
        print(res)


    def string_del(self):
        self.r.delete("user2")

def main():
    r = TestString()
    # 设置单个值
    r.set_string("user", "muyi")
    r.get_string()

    # 设置多个值
    r.string_mset()
    r.string_mget()

    # 删除
    r.string_del()


if __name__ == '__main__':
    main()

图片.png

列表相关操作

import redis

class ListOperation:
    def __init__(self):
        self.r = redis.StrictRedis(host='localhost', port=6379)


    def list_push(self):
        """
        向Redis列表中添加元素
        """
        res = self.r.lpush('common','1')
        res = self.r.rpush('common','2')

    def list_pop(self):
        """
        从Redis列表中移除并获取元素
        """
        res = self.r.lpop('common')
        res = self.r.rpop('common')

    def list_range(self):
        """
        获取Redis列表中指定范围内的元素
        """
        res = self.r.lrange('common',0,-1)
        print(res)


def main():
    l = ListOperation()
    # 插入记录
    l.list_push()
    # 获取记录
    l.list_range()
    # 移除记录
    l.list_pop()


if __name__ == '__main__':
    main()

集合相关操作

import redis


class SetOperation(object):
    def __init__(self):
        self.r = redis.StrictRedis(host='localhost', port=6379)

    def add_set(self):
        """
        向Redis集合中添加元素。
        """
        res = self.r.sadd('set1', '1', '2')
        lis = ['Cat', 'Dog']
        # 使用解包操作符 * 将列表中的每个元素传递给 sadd 方法
        res = self.r.sadd('set2', *lis)

    def del_set(self):
        """
        从Redis集合中删除元素。
        """
        res = self.r.srem('set1', 1)
        print(res)

    def del_pop(self):
        res = self.r.spop('set2')
        print(res)


def main():
    s = SetOperation()
    # 添加数据
    s.add_set()

    # 删除数据
    s.del_set()

    # 随机弹出一个元素
    s.del_pop()


if __name__ == '__main__':
    main()

图片.png

哈希相关操作

import redis

class HashOperation(object):
    def __init__(self):
        self.r = redis.StrictRedis(host='127.0.0.1',port=6379)


    def set_hash(self):
        """
        批量设置hash
        :return:
        """
        dic = {
            'id':1,
            'name':'huawei'
        }
        res = self.r.hset('mobile',mapping=dic)

    def get_hash(self):
        """
        获取hash
        :return:
        """
        res = self.r.hgetall('mobile')

    def hash_hexists(self):
        """
        判断某个key是否存在于hash中
        :return:
        """
        res = self.r.hexists('mobile','id')
        print(res)

def main():
    h = HashOperation()
    h.set_hash()
    h.get_hash()
    h.hash_hexists()

if __name__ == '__main__':
    main()

图片.png