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 # [email protected] coralshanshan # 601601601 # [email protected] pengfeihuchao # woaidami # [email protected] simulategirl # @#$9608125 # [email protected] daisypp # 12345678 # [email protected] sirenxing424 # tfiloveyou # [email protected] raininglxy # 1901061139 # [email protected] leochenlei # leichenlei # [email protected] z370433835 # lkp145566 # [email protected]
--创建命令脚本
***:~/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/