• python:redis简单操作


    一,安装redis-py

    • pip install redis
    • easy_install redis

    二,简单用法

    import redis
    
    
    # 连接redis服务器
    def conn_redis():
        r = redis.StrictRedis(host='10.21.25.196', port=6379, db=0)
        return r
    
    #hash
    def test_hash():
        r = conn_redis()  # 连接redis数据库
        result = r.hgetall('1861098')  # 获取该字典中所有k-V对
        print result.keys()  # 获取所有key
        print result.values()  # 获取所有value
        for key, value in  result.items():  # 遍历key,value对
            print key, '------>', value
        print result.items()  # 输出所有key,value对(以元组的方式)
        print result.has_key('count')  # 判断时候存在某个键值
        return result
        
        
    def test_set():
        r = conn_redis()  # 连接redis数据库
        result = r.smembers('test')  # 获取该集合中所有元素
    #     print len(result)#求集合长度
    #     print list(result)#将set转化成list
    #     for key in result:
    #         print key
        
        return list(result)
    
    def test_simple():
        r = conn_redis()
    #     r.set('foo','bar')
        print r.get('foo')
        
    def test_pipe():
        r = conn_redis()
        pipe = r.pipeline(transaction=False)
        result = pipe.set('foo', 'lky').get('foo').execute()
        print result
        
    def test_transaction():
        r = conn_redis()
        pipe = r.pipeline()
        pipe.set('foo', 'bar')
        pipe.get('foo')
        result = pipe.execute()
        print result
  • 相关阅读:
    记录犯得最可笑的错误
    爬虫阶段内容总结
    docker_nginx_Elasticsearch
    git基础
    爬虫pearPro
    爬虫wangyiPro
    sunPro
    docker-compose终极搞定个人博客
    小程序下拉三个小点不显示问题
    vue鼠标拖动
  • 原文地址:https://www.cnblogs.com/dmir/p/5023359.html
Copyright © 2020-2023  润新知