美文网首页
Redis:Python 操作 Redis 常用示例

Redis:Python 操作 Redis 常用示例

作者: dex0423 | 来源:发表于2020-08-06 10:55 被阅读0次
import redis

class RedisBase:

    def __new__(cls, *args, **kwargs):
        """单例模式"""
        if not hasattr(cls, "instance"):
            cls.instance = super(RedisBase, cls).__new__(cls, *args, **kwargs)
        return cls.instance

    def __init__(self):
        """每一个数据库实例管理一个连接池"""
        pool = redis.ConnectionPool(host='192.168.0.104',port=6379,db=0,password='bright123')
        self.r=redis.Redis(connection_pool=pool)

class RedisString(RedisBase):

    def set(self):
        """设置值"""
        result=self.r.set('widh',58)
        return result

    def get(self):
        """获取值"""
        result=self.r.get('width')
        return result

    def mset(self):
        """设置多个键值对"""
        result=self.r.mset(
            {
                'salary1':5000,
                'salary2':6000
            }
        )
        return result

    def mget(self):
        """获取多个值"""
        result=self.r.mget(
            [
                'salary1',
                'salary2'
            ]
        )
        return result

    def delete(self):
        """删除键值"""
        result=self.r.delete('salary1')
        return result

    def incr(self):
        result=self.r.incr('salary1',20)
        return result

class RedisList(RedisBase):

    def lpush(self):
        result=self.r.lpush(
            'l1',*[1,2,3,4,5]
        )
        return result

    def lpop(self):
        """移除最左边的数据并返回"""
        result=self.r.lpop('l1')
        return result

    def lrange(self):
        """获取指定长度的数据"""
        result=self.r.lrange('l1',0,-1)
        return result

class RedisSet(RedisBase):

    def sadd(self):
        """添加集合,返回的是成员的个数"""
        result=self.r.sadd('set1',*[1,2,3,4,5])
        return result

    def sismember(self):
        """判断一个成员是否是集合的成员,返回布尔值"""
        result=self.r.sismember('set1',2)
        return result

    def smembers(self):
        """返回集合的所有成员"""
        result=self.r.smembers('set1')
        return result

    def sdiff(self):
        """返回与其它集合的差异元素"""
        result=self.r.sdiff('s5','s7')
        return result

    def sinder(self):
        """返回几个集合的交集"""
        result=self.r.sinter('s5','s7')
        return result

    def sunion(self):
        """返回几个集合的并集"""
        result=self.r.sunion('s5','s7')
        return result

def main():
    rs=RedisString()
    rs1=RedisString()
    rl=RedisList()
    rl1=RedisList()
    rs=RedisSet()
    rs1=RedisSet()
    print(rs)
    print(rs1)
    print(rl)
    print(rl1)
    print(rs)
    print(rs1)
    # rs.set()
    # rs.mset()
    # print( rs.mget())
    # print(rs.delete())
    # print(rs.incr())
    # print(rl.lpush())
    # print(rl.lpop())
    # print(rl.lrange())
    # print(rs.sadd())
    # print(rs1.sismember())
    # print(rs.smembers())
    # print(rs.sdiff())
    # print(rs.sinder())
    print(rs.sunion())

if __name__ == '__main__':
    main()

相关文章

网友评论

      本文标题:Redis:Python 操作 Redis 常用示例

      本文链接:https://www.haomeiwen.com/subject/ldfzrktx.html