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/
Python连接Redis连接配置的更多相关文章
- 使用可视化工具redisclient连接redis
可视化工具推荐:http://database.51cto.com/art/201505/477692.htm 1.连接redis服务端 1.1 设置连接密码:在redis根目录下,双击redis-c ...
- python之redis和memcache操作
Redis 教程 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据 ...
- python 操作 Redis
目录 Redis 模块基本介绍 参考 redis redis-py 的 API 连接 redis 普通连接 连接池 redis 字符串操作 单次设置key-value 批量设置key-value re ...
- [ecmagent][redis学习][1初识redis] python操作redis
#1 连接redis # 连接redis -- import redis -- 使用端口连接redis conn = redis.Redis(host=) -- 使用套接字连接 r = redis.R ...
- redis基础之python连接redis(五)
前言 前面介绍了在数据库命令行直接操作redis,现在学习使用python的redis包来操作redis,本人安装的是redis==2.10.6: 系列文章 redis安装和配置 redis命令行操作 ...
- Python远程连接Redis
import redisr=redis.Redis(host='192.168.56.102',port=6379,db=0,password='jinxfredis' )r.set('name',' ...
- python连接redis、redis字符串操作、hash操作、列表操作、其他通用操作、管道、django中使用redis
今日内容概要 python连接redis redis字符串操作 redis之hash操作 redis之列表操作 redis其他 通用操作,管道 django中使用redis 内容详细 1.python ...
- Python操作Redis及连接方式
前沿:随着互联网的高速发展,数据变得越来越重要,Python成为了人工智能的热门语言,而Nosql数据库已成为日常开发用品. 今天要写的是Python操作Redis Redis的安装我就不介绍了,你可 ...
- python操作Redis安装、支持存储类型、普通连接、连接池
一.python操作redis安装和支持存储类型 安装redis模块 pip3 install redis 二.Python操作Redis之普通连接 redis-py提供两个类Redis和Strict ...
随机推荐
- 真相:中国版BBB用USB连电脑没有盘符的根本原因分析
很多网友在问:为什么中国版的装完驱动插上板子没有显示端口号和69M的盘符??楼主发现,在开机启动的时候,加载g_multi模块时出现错误提示 invalid argument. Emb ...
- vim乱码处理
编辑~/.vimrc文件,加上如下几行: set fileencodings=utf-8set termencoding=utf-8set encoding=utf-8
- Flash学习初总结
话说尝试了一周多的Flash编程,有些理解为什么很多程序员都不喜欢用Flash编程了. 首先,就是没有编程的难度,想要编好Flash,也就是有良好的视觉效果,那么关键点不在你的程序逻辑或者代码条理上, ...
- 使用logminer分析日志文件
实验环境 win7 64 oracle PL/SQL Release 11.2.0.1.0 - Productionhttp://blog.csdn.net/tianlesoftware/artic ...
- javascript进击(八)JSON
JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Ob ...
- C# ADO.NET参数查询
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- display:none和visibility:hidden的区别[]
display:none和visibility:hidden都是把网页上某个元素隐藏起来的功能,但两者有所区别,我发现使用 visibility:hidden属性会使对象不可见,但该对象在网页所占的空 ...
- 使用solr搭建你的全文检索
Solr 是一个可供企业使用的.基于 Lucene 的开箱即用的搜索服务器.对Lucene不熟?那么建议先看看下面两篇文档: 实战Lucene,第 1 部分: 初识 Lucene:http://www ...
- 节点插入--对比jQuery和JavaScript方法(一)
二.插入元素: 1 <div> 2 <p>面朝大海,春暖花开</p> 3 </div> (一).jQuery方法 1.在节点内部插入: 方法 说明 ap ...
- webstorm 快捷键
Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*-*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 Ctrl+D 复制行 Ctrl+G 查找行 Ctrl+Shift+Up ...