Python系列之Redis--02.Redis与Python操作Redis

1 . Redis 简介

Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库
Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。它支持字符串、哈希表、列表、集合、有序集合,位图,hyperloglogs等数据类型。内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。

  1. 资料
    官网:http://www.redis.net.cn/
    文档:http://redis-py.readthedocs.io/en/latest/
    github:https://github.com/andymccurdy/redis-py
    Pypi:https://pypi.python.org/pypi/redis

2 . Python操作Redis

  1. String字符串操作

        """
    set  -- 设置值
    get -- 获取值
    mset  -- 设置多个键值对
    mget  -- 获取多个键值对
    append -- 添加字符串
    del -- 删除
    incr/decr   -- 增加/减少 1
    
    class TestString(object):
    
    # 初始化的时候连接Redis
    
        def __init__(self):
            self.r = redis.StrictRedis( 
                            host='localhost', 
                            port=6379,  
                            db=0)
    
    
    # 新增
    
    def test_set(self):
        rest = self.r.set('user2', 'amy')
        print(rest)
    
    
    # 查询
    
    def test_get(self):
        rest = self.r.get('user4')
        print(rest)
        return rest
    
    
    # 批量新增
    
    def test_mset(self):
        # 字典类型  key-value
        d = {
            'user3': 'bob',
            'user4': 'box'
        }
        rest = self.r.mset(d)
        print(rest)
    
    
    # 批量查询
    
    def test_mget(self):
        # list列表类型 key,key
        d = [
            'user3',
            'user4'
        ]
        rest = self.r.mget(d)
        print(rest)
        return rest
    
    
    # 删除
    
    def test_del(self):
        rest = self.r.delete('user4')
        print(rest)
    
  2. List类型操作

     class TestList(object):
    """
    lpush/rpush  -- 从左/右插入数据
    lrange -- 获取指定长度的数据
    ltrim -- 截取一定长度的数据
    lpop/rpop --移除最左/右的元素并返回
    lpushx/rpushx -- key存在的时候才插入数据,不存在时不做任何处理
    
    """
    
    def __init__(self):
        self.r = redis.Redis(host='localhost', port=6379, db=0)
    
    def test_push(self):
        ''' lpush/rpush  -- 从左/右插入数据 '''
        t = ['Amy', 'Jhon']
        rest = self.r.lpush('l_eat3', *t)
        print(rest)
        rest = self.r.lrange('l_eat3', 0, -1)
        print(rest)
    
    def test_pop(self):
        ''' lpop/rpop --移除最左/右的元素并返回 '''
        rest = self.r.lpop('l_eat3')
        print(rest)
        rest = self.r.lrange('l_eat3', 0, -1)
        print(rest)
  3. Hash类型操作

     class TestHash(Base):
     '''Base函数为连接数据库的类,封装为一个类就不用一直写了'''
    """
    hset/hget --设置/获取散列值
    hmset/hmget -- 设置/获取多对散列值
    hsetnx -- 如果散列已经存在,则不设置
    hkeys/hvals -- 返回所有Keys/Values
    hlen -- 返回散列包含域(field)的数量
    hdel -- 删除散列指定的域(field)
    hexists -- 判断是否存在
    """
    
    def test_set(self):
        ''' hset/hget --设置/获取散列值 '''
        rest = self.r.hset('stu:xxx02', 'name', 'Jhon')
        print(rest)
        rest = self.r.hexists('stu:xxx02', 'name')
        print(rest)
        rest = self.r.hget('stu:xxx02', 'name')
        print(rest)
    
    def test_mset(self):
        ''' hmset/hmget -- 设置/获取多对散列值 '''
        m = {
            'name': 'Bob',
            'age': 21,
            'grade': 98
        }
        rest = self.r.hmset('stu:xxx03', m)
        print(rest)
        rest = self.r.hkeys('stu:xxx03')
        print(rest)

猜你喜欢

转载自blog.csdn.net/prospective0821/article/details/79958016