美文网首页我爱编程
数据库笔记(10)- Python 操作Redis

数据库笔记(10)- Python 操作Redis

作者: 逍遥_yjz | 来源:发表于2018-06-18 21:57 被阅读0次

    Python 操作Redis


    1. 安装及连接

    Pip install redis
    

    连接

    import redis
    
    # 加入Strict 严格上的,
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    # 兼容一些老版本啊
    #r = redis.Redis(host='localhost', port=6379, db=0)
    
    user1 = r.get('user1')
    print(user1) # b'Jhon'
    

    2. 字符串的操作

    自带一些连接池

    class TestString(object):
    
        ""
        def __init__(self):
            self.r = redis.StrictRedis(host='localhost', port=6379, db=0)
    
        def test_set(self):
            ''' set  -- 设置值'''
            rest = self.r.set('user2','Amy')
            print(rest)
            return rest
    
        def test_get(self):
            ''' get  -- 获取值'''
            rest = self.r.get('user3')
            print(rest)
            return rest
    
        def test_mset(self):
            ''' mset -- 设置多个键值对'''
            d = {
                'user3':'Bob',
                'user4':'Bobx'
            }
            rest = self.r.mset(d)
            print(rest)
            return rest
    
        def test_mget(self):
            '''mget == 获取多个键值对'''
            l = ['user3','user4']
            rest = self.r.mget(l)
            print(rest)
            return rest
    
        def test_del(self):
            '''del == 删除'''
            rest = self.r.delete('user3')
            print(rest)
    
    def main():
        str_obj = TestString()
        # str_obj.test_set()
        str_obj.test_get()
        #str_obj.test_mset()
        #str_obj.test_mget()
        #str_obj.test_del()
    
    

    3. 列表(List)的操作

    class TestList(object):
    
        def __init__(self):
            self.r =  redis.StrictRedis(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)
    
    

    4. 集合(Set)的操作

    # coding:utf-8
    
    import redis
    
    class Base(object):
        def __init__(self):
            self.r =  redis.StrictRedis(host='localhost', port=6379, db=0)
    
    class TestString(object):
    
        ""
        def __init__(self):
            self.r = redis.StrictRedis(host='localhost', port=6379, db=0)
    
        def test_set(self):
            ''' set  -- 设置值'''
            rest = self.r.set('user2','Amy')
            print(rest)
            return rest
    
        def test_get(self):
            ''' get  -- 获取值'''
            rest = self.r.get('user3')
            print(rest)
            return rest
    
        def test_mset(self):
            ''' mset -- 设置多个键值对'''
            d = {
                'user3':'Bob',
                'user4':'Bobx'
            }
            rest = self.r.mset(d)
            print(rest)
            return rest
    
        def test_mget(self):
            '''mget == 获取多个键值对'''
            l = ['user3','user4']
            rest = self.r.mget(l)
            print(rest)
            return rest
    
        def test_del(self):
            '''del == 删除'''
            rest = self.r.delete('user3')
            print(rest)
    
    class TestList(object):
    
        def __init__(self):
            self.r =  redis.StrictRedis(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)
    
    class TestSet(Base):
    
        def test_sadd(self):
            ''' sadd  -- 添加元素'''
            l = ['Cat','Dog']
            rest = self.r.sadd('zoo3',*l)
            print(rest)
            rest = self.r.smembers('zoo3')
            print(rest)
    
        def test_srem(self):
            ''' srem  === 删除元素'''
            rest = self.r.srem('zoo2', 'Dog')
            print(rest)
            rest = self.r.smembers('zoo2')
            print(rest)
    
        def test_sinter(self):
            ''' sinter --- 返回几个集合的交集'''
            rest = self.r.sinter('zoo2','zoo3')
            print(rest)
    
    
    
    
    
    def main():
        str_obj = TestString()
        # str_obj.test_set()
        #str_obj.test_get()
        #str_obj.test_mset()
        #str_obj.test_mget()
        #str_obj.test_del()
    
        # 列表
        #list_obj = TestList()
        #list_obj.test_push()
    
        #list_obj.test_pop()
    
        # 集合
        set_obj = TestSet()
        #set_obj.test_sadd()
        # set_obj.test_srem()
        set_obj.test_sinter()
    
    
    
    if __name__ == '__main__':
        main()
    
    

    数据库笔记(1)-简述
    数据库笔记(2)- Mysql 基础
    数据库笔记(3)-ORM
    数据库笔记(4)-网易新闻的实战
    数据库笔记(5)- MongoDB基础
    数据库笔记(6)- Python 操作MongoDB
    数据库笔记(7)- MongoDB ORM
    数据库笔记(8)- 网易新闻实战
    数据库笔记(9)- Redis 数据库基础
    数据库笔记(10)- Python 操作Redis

    相关文章

      网友评论

        本文标题:数据库笔记(10)- Python 操作Redis

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