python简单操作redis
来源:互联网 发布:梦里花落知多少陈曦 编辑:程序博客网 时间:2024/05/21 06:21
转载自:http://the5fire.com/python-simple-redis.html
python redis 安装
wget http://redis.googlecode.com/tar xzf redis-2.x.xmake && make test#不过可能会报错,百度一下就可以了,不是很难解决的sudo easy_install redis
git clone https://github.com/andymccurdy/redis-py.git
cd redis-py
python setup.py install
# Parser安装
# Parser可以控制如何解析redis响应的内容。redis-py包含两个Parser类,PythonParser和HiredisParser。
# 默认,如果已经安装了hiredis模块,redis-py会使用HiredisParser,否则会使用PythonParser。
# HiredisParser是C编写的,由redis核心团队维护,性能要比PythonParser提高10倍以上,所以推荐使用。安装方法,使用easy_install:
easy_install hiredis
关于redis的复杂的使用以后用到再来学习,代码更直观:
import rediscache = redis.StrictRedis(host='localhost', port=6379)#1. 简单的get和set操作print u'====set操作:'cache.set('blog:title', u'the5fire的技术博客')print cache.get('blog:title')#真实应用场景,批量set和getfor i in range(10): cache.mset({ 'blog:post:%s:title' % i: u'文章%s标题' % i, 'blog:post:%s:content' % i: u'文章%s的正文' % i })post_list = []for i in range(10): post = cache.mget('blog:post:%s:title' % i, 'blog:post:%s:content' % i) if post: post_list.append(post)for title, content in post_list: print title, content#2、 hashed类型的操作print u'====hashed操作:'cache.hset('blog:info','title', u'the5fire的技术博客')cache.hset('blog:info','url', u'http://www.the5fire.com')blog_info_title = cache.hget('blog:info', 'title')print blog_info_titleblog_info = cache.hgetall('blog:info')print blog_info #同样hashed类型的set和get也可以进行批量操作cache.hmset('blog:info', { 'title': 'the5fire blog', 'url': 'http://www.the5fire.com', })blog_info1 = cache.hmget('blog:info', 'title', 'url')print blog_info1#3、lists类型的操作print u'====lists操作:'cache.lpush('blog:tags', 'python')cache.lpush('blog:tags', 'linux')tags = cache.lrange('blog:tags', 0, 2)print tags#对应的还有rpush,lpop,rpop,更多可以看红丸的redis实战#4、sets类型的操作print u'====sets操作:'cache.sadd('blog:category:python', '001')cache.sadd('blog:category:python', '002')#cache.sadd('blog:category:python', '001', '002')print cache.smembers('blog:category:python')cache.srem('blog:category:python', '001')print cache.smembers('blog:category:python')
- python简单操作redis
- python简单操作redis
- python简单操作redis
- python操作redis简单例子
- python操作redis简单例子
- python操作redis简单例子
- python操作redis简单例子
- python操作redis简单例子
- python操作redis简单例子
- python操作redis简单例子
- redis -- python操作连接redis简单示例
- python操作redis简单例子<转>
- Python 线程和 redis 简单读写操作应用
- redis -- python操作连接redis
- Redis简单操作
- redis一些简单操作
- Redis 简单启动操作
- Redis---- php简单操作
- mysql数据库 连接myeclipse问题终于解决了
- 10月18日
- Android支付接入(二):移动游戏基地
- SCSF - Part 6 Constructor Injection in the CAB
- jsonview json查看工具
- python简单操作redis
- 选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法,而冒泡排序、插入排序、归并排序和基数排序
- 安卓开发之JNI编程详解
- Effective STL 12 STL 使用reserve来避免不必要的重新分配
- java作业:使一串数字循环输出
- Windows phone UI流畅度优化
- windows下操作mysql
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- samba文件夹权限设置