Python操作redis
2010-12-28 13:40
423 查看
首先确保redis已经正常启动。
安装
可以去pypi上找到redis的Python模块:
http://pypi.python.org/pypi?%3Aaction=search&term=redis&submit=search
然后按照提示down下来redis-py-2.2.1.tar.gz
非常标准的解压: #tar xvzf redis-py-2.2.1.tar.gz
进入解压目录,进行Python模块的标准安装:
python setup.py install
运行
打开Python解释器:
>>> import redis
>>> r = redis.Redis(host='localhost', port=6379, db=0) #如果设置了密码,就加上password=密码
>>> r.set('foo', 'bar') #或者写成 r['foo'] = 'bar'
True
>>> r.get('foo')
'bar'
>>> r.delete('foo')
True
>>> r.dbsize() #库里有多少key,多少条数据
0
>>> r['test']='OK!'
>>> r.save() #强行把数据库保存到硬盘。保存时阻塞
True
--------------------------------
>>> r.flushdb() #删除当前数据库的所有数据
True
>>> a = r.get('chang')
>>> a # 因为是Noen对象,什么也不显示!
>>> dir(a)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> r.exists('chang') #看是否存在这个键值
False
>>> r.keys() # 列出所有键值。(这时候已经存了4个了)
['aaa', 'test', 'bbb', 'key1']
附注A:
来看一下redis.Redis的 init() 函数定义:
__init__(self, host='localhost', port=6379, db=0, password=None, socket_timeout=None, connection_pool=None, charset='utf-
8', errors='strict', decode_responses=False, unix_socket_path=None)
最新的redis 2.6.0加入了连接池,具体用法可以看作者博客。
附注B:
其他命令API,请参照redis-Python作者的博客,写的挺清楚了:
https://github.com/andymccurdy/redis-py
安装
可以去pypi上找到redis的Python模块:
http://pypi.python.org/pypi?%3Aaction=search&term=redis&submit=search
然后按照提示down下来redis-py-2.2.1.tar.gz
非常标准的解压: #tar xvzf redis-py-2.2.1.tar.gz
进入解压目录,进行Python模块的标准安装:
python setup.py install
运行
打开Python解释器:
>>> import redis
>>> r = redis.Redis(host='localhost', port=6379, db=0) #如果设置了密码,就加上password=密码
>>> r.set('foo', 'bar') #或者写成 r['foo'] = 'bar'
True
>>> r.get('foo')
'bar'
>>> r.delete('foo')
True
>>> r.dbsize() #库里有多少key,多少条数据
0
>>> r['test']='OK!'
>>> r.save() #强行把数据库保存到硬盘。保存时阻塞
True
--------------------------------
>>> r.flushdb() #删除当前数据库的所有数据
True
>>> a = r.get('chang')
>>> a # 因为是Noen对象,什么也不显示!
>>> dir(a)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> r.exists('chang') #看是否存在这个键值
False
>>> r.keys() # 列出所有键值。(这时候已经存了4个了)
['aaa', 'test', 'bbb', 'key1']
附注A:
来看一下redis.Redis的 init() 函数定义:
__init__(self, host='localhost', port=6379, db=0, password=None, socket_timeout=None, connection_pool=None, charset='utf-
8', errors='strict', decode_responses=False, unix_socket_path=None)
最新的redis 2.6.0加入了连接池,具体用法可以看作者博客。
附注B:
其他命令API,请参照redis-Python作者的博客,写的挺清楚了:
https://github.com/andymccurdy/redis-py
相关文章推荐
- python操作redis
- Python——redis操作手册
- 使用 python 来操作 redis 用法详解
- python对redis的常用操作 上 (对列表、字符串、散列结构操作)
- 使用Python操作Redis
- python操作redis
- 使用 python 操作 redis
- python操作redis的方法
- Python操作Redis
- python操作redis和mysql方法
- python 实现redis 操作API 接口,提供其他程序调用
- python操作redis操作方法实例
- Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
- 使用Python操作Redis
- Python操作Redis数据库
- python中Redis安装及基本操作
- 使用Python操作Redis
- Python操作Redis数据库
- python操作redis
- Python 操作 Redis