• Python连接Redis连接配置


    1. 测试连接:

    Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
    [GCC 4.9.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import redis
    >>> redisClient = redis.StrictRedis(host='127.0.0.1',port=6379,db=0)
    >>> redisClient.set('test_redis','Hello wyl')
    True>>> value = redisClient.get('test_redis')
    >>> print value
    Hello wyl>>> redisClient.delete('test_redis')
    1
    >>> value = redisClient.get('test_redis')
    >>> print value
    None
    >>> 

    在此之前必须确保python和redis正确安装和配置.

    >>> dir(redis)
    ['AuthenticationError', 'BlockingConnectionPool', 'BusyLoadingError', 'Connection', 'ConnectionError', 'ConnectionPool', 'DataError', 'InvalidResponse', 'PubSubError', 'ReadOnlyError', 'Redis', 'RedisError', 'ResponseError', 'SSLConnection', 'StrictRedis', 'TimeoutError', 'UnixDomainSocketConnection', 'VERSION', 'WatchError', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_compat', 'client', 'connection', 'exceptions', 'from_url', 'lock', 'utils']

    2. 测试实例:

    (1).把文本数据导入到redis:

    ***:~/Redis$ more redis.txt
    wolys # wolysopen111 # wolys@21cn.com
    
    coralshanshan # 601601601 # zss1984@126.com
    
    pengfeihuchao # woaidami # 294522652@qq.com
    
    simulategirl # @#$9608125 # simulateboy@163.com
    
    daisypp # 12345678 # zhoushigang_123@163.com
    
    sirenxing424 # tfiloveyou # sirenxing424@126.com
    
    raininglxy # 1901061139 # lixinyu23@qq.com
    
    leochenlei # leichenlei # chenlei1201@gmail.com
    
    z370433835 # lkp145566 # 370433835@qq.com

    --创建命令脚本

    ***:~/Redis$ cat imp_red.py 
    
    import redis
    
    import re
    
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
    
    r = redis.Redis(connection_pool=pool)
    
    pipe = r.pipeline()
    
    p=re.compile(r'(.*)s#s(.*)s#s(.*)');
    
    pipe = r.pipeline()
    
    f = open("redis.txt")
    
    matchs=p.findall(f.read())
    
    for user in matchs:
    
       key='users_%s' %user[0].strip()
    
       pipe.hset(key,'pwd',user[1].strip()).hset(key,'email',user[2].strip())
    
    pipe.execute()
    
    f.close()

    执行脚本:

    ***:~/Redis$ python imp_red.py 

    查看redis数据:

    ***:~/Redis$ redis-cli 
    127.0.0.1:6379> keys *
    1) "users_pengfeihuchao"
    2) "users_coralshanshan"
    3) "users_leochenlei"
    4) "users_sirenxing424"
    5) "users_z370433835"
    6) "users_raininglxy"
    7) "users_daisypp"
    8) "users_wolys"
    9) "users_simulategirl"
    127.0.0.1:6379> 

    以上知识参考文章:http://blog.csdn.net/lichangzai/article/details/8701562

    3. redis的简单操作:

    redis连接实例是线程安全的,可以直接将redis连接实例设置为一个全局变量,直接使用。如果需要另一个Redis实例(or Redis数据库)时,就需要重新创建redis连接实例来获取一个新的连接。同理,python的redis没有实现select命令。

    Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
    [GCC 4.9.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import redis
    >>> r = redis.Redis(host='127.0.0.1', port = 6379, db=0)
    >>> r.set('wu','yanlong')
    True
    >>> r.get()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: get() takes exactly 2 arguments (1 given)
    >>> r.get('wu')
    'yanlong'
    >>> r['wu']
    'yanlong'
    >>> r.keys()
    ['users_pengfeihuchao', 'users_coralshanshan', 'users_leochenlei', 'users_sirenxing424', 'users_z370433835', 'users_raininglxy', 'wu', 'users_daisypp', 'users_wolys', 'users_simulategirl']
    >>> r.keys('wu')
    ['wu']
    >>> r.dbsize()
    10L
    >>> r.delete('wu')
    1
    >>> r.keys()
    ['users_pengfeihuchao', 'users_coralshanshan', 'users_leochenlei', 'users_sirenxing424', 'users_z370433835', 'users_raininglxy', 'users_daisypp', 'users_wolys', 'users_simulategirl']
    >>> r.dbsize()
    9L
    >>> r.save()
    True
    >>> r.get('wu')
    >>> r.flushdb()
    True
    >>> r.keys()
    []
    >>> 

    4. pipeline()操作:

    管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。

    >>> p = r.pipeline() #创建一个管道
    >>> p.set('hello','redislearn')
    Pipeline<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>
    >>> p.sadd('fff','sss')
    Pipeline<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>
    >>> p.incr('num')
    Pipeline<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>
    >>> p.excute()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Pipeline' object has no attribute 'excute'
    >>> p.execute()
    [True, 1, 1]
    >>> r.get('hello')
    'redislearn'
    >>> 

    管道的命令可以写在一起,如:

         
    >>> p.set('hello','redis').p.sadd('faz','baz').incr('num').execute()

    默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。

    以上部分参考:http://debugo.com/python-redis/

  • 相关阅读:
    vue-cli webpack3扩展多模块打包
    性能对比:aelf智能合约运行环境性能是evm的1000倍
    aelf帮助C#工程师10分钟零门槛搭建DAPP&私有链开发环境
    ASP.NET Core依赖注入最佳实践,提示&技巧
    linux环境安装php fileinfo扩展
    简单的部署nodejs和web应用
    安卓NDK操作sqlite3
    [解决]微信小程序调用wx.login解密之后出现乱码的问题
    windows server 2008 iis 添加ssl证书
    Mac 下修改文件或文件夹的隐藏状态
  • 原文地址:https://www.cnblogs.com/blogofwyl/p/4609877.html
Copyright © 2020-2023  润新知