Python redis库的一些操作

# -*- coding: utf-8 -*-

import redis
from loguru import logger

class RedisCline(object):

    def __init__(self, host='127.0.0.1', port=6379, db=10):

        pool = redis.ConnectionPool(host=host, port=port, db=db)
        self.conn_redis = redis.Redis(connection_pool=pool)

    ## set 集合
    
    def get_conn_redis(self):
        
        return self.conn_redis
    
    # 新增
    def set_sadd(self, db_name, item):
        rest = self.conn_redis.sadd(db_name, item)
        return rest

    # 判断 member 元素是否是集合 key 的成员
    def set_sismember(self, db_name, item):
        rest = self.conn_redis.sismember(db_name, item)
        return rest

    # 增量式新增
    def set_sadd_zengliang(self, db_name_uuid, item_uuid,db_name, item):

        rest = self.set_sadd(db_name_uuid, item_uuid)
        if rest == 0:
            return False
        # elif rest == 1:
        #     self.set_sadd(db_name, item)

        else:
            logger.warning(f'redis 增量爬取 有问题!!!!!!!!!')

    # 移除并返回集合中的一个随机元素
    def set_spop(self, db_name):
        data = self.conn_redis.spop(db_name)
        return data

    # Redis 集合(Set)
    def list_push(self, db_name, item, fanfgshi='L'):

        if fanfgshi == 'L':
            self.conn_redis.lpush(db_name, item)
        elif fanfgshi == "R":
            self.conn_redis.rpush(db_name, item)
        else:
            logger.warning(f'redis 插入数据 有问题!!!!!!!!!')

    def list_pop(self, db_name, fanfgshi='R'):

        if fanfgshi == 'L':
            data = self.conn_redis.lpop(db_name)
        elif fanfgshi == 'R':
            data = self.conn_redis.rpop(db_name)
        elif fanfgshi == 'BL':
            data = self.conn_redis.blpop(db_name)
        elif fanfgshi == "RL":
            data = self.conn_redis.brpop(db_name)
        else:
            logger.warning(f'redis 取出 有问题!!!!!!!!!')
            data = None
        return data

    def list_llen(self, db_name):
        data = self.conn_redis.llen(db_name)
        return data
    def list_lrange(self, db_name, start, stop):
        data = self.conn_redis.lrange(db_name, start, stop)
        return data

你可能感兴趣的:(python,redis,python)