python方法操作redis

将redis的keys和values的一些数据结构扫了一遍,跃跃欲试code里访问一把,由于基本所有编程语言接口redis都支持,就还是找个使用性和可读性比较优雅的python来试水

下载安装python模块

wget https://pypi.python.org/packages/source/r/redis/redis-2.10.3.tar.gz
tar zxvf redis-2.10.3.tar.gz
cd redis-2.10.3
python setup.py install

模块源代码里INSTALL文档写的很清楚,直接install即可;但是奇怪的是验证一下居然报错

[lihui@localhost ~]# cat myredis.py 
#!/usr/bin/env python

import redis
r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0)
r.set('lihui', '30')

[lihui@localhost ~]# python myredis.py 
Traceback (most recent call last):
  File "/home/lihui/myredis.py", line 3, in <module>
    import redis
  File "build/bdist.linux-x86_64/egg/redis/__init__.py", line 1, in <module>
  File "build/bdist.linux-x86_64/egg/redis/client.py", line 6, in <module>
  File "/usr/lib64/python2.6/threading.py", line 45, in <module>
    module='threading', message='sys.exc_clear')
  File "/usr/lib64/python2.6/warnings.py", line 50, in filterwarnings
    import re
  File "/home/lihui/re.py", line 5, in <module>
    #
AttributeError: 'module' object has no attribute 'StrictRedis'

看这样子,好像没安装成功,但是更让人纳闷的是,切换称root,换个目录交互式又不会报错

[root@localhost ~]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.StrictRedis(host = 'localhost', port = 6379, db = 0)
>>> r.set('lihui', '30')
True
>>> r.get('lihui')
'30'
>>> 

突然之间感到十分惆怅,什么玩意?仔细审查错误信息中,re.py映入了眼帘,这个文件已经重命名了,为什么还会这个文件报错,难道有中间文件?

[lihui@localhost ~]# ll /home/lihui/re.pyc 
-rw-r--r--. 1 lihui lihui 277 Feb 13 20:48 /home/lihui/re.pyc
[lihui@localhost ~]# date
Fri Feb 13 20:54:49 CST 2015

OMG,果然不出所料,pyc是二进制文件,主要是加载速度快,执行这个python脚本居然会pyc也以文件形式存在,不用犹豫删掉他肯定就对了

[lihui@localhost ~]# rm -f /home/lihui/re.pyc
[lihui@localhost ~]# python /home/lihui/myredis.py 
True

终于执行正确,下面验证下数据库添加的这个key和value

[lihui@localhost ~]# python myredis.py 
True
[root@localhost ~]# redis-cli 
127.0.0.1:6379> keys *
1) "lihui"
127.0.0.1:6379> sget lihui
(error) ERR unknown command 'sget'
127.0.0.1:6379> get lihui
"30"
127.0.0.1:6379> 

可见直接就可以操作redis数据库,而且是十分方便,并且python模块里的方法也跟redis数据结构的操作的方法名一样,这也许就是为什么python惹人喜欢的原因

其它还有不明白的可以查看官方网页:https://pypi.python.org/pypi/redis/2.10.3

上面的列子中可以看出,仅仅通过下面这一句

r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0)

就能通过对象 r 来随意操作数据库,来找找StrictRedis是啥子玩意,如何定义的

[lihui@localhost ~]# grep StrictRedis /home/lihui/redis-2.10.3/* -R
/home/lihui/redis-2.10.3/build/lib/redis/__init__.py:from redis.client import Redis, StrictRedis
/home/lihui/redis-2.10.3/build/lib/redis/__init__.py:    'Redis', 
/home/lihui/redis-2.10.3/build/lib/redis/client.py:class StrictRedis(object):
/home/lihui/redis-2.10.3/build/lib/redis/client.py:class Redis(StrictRedis):

找到client.py查看

[lihui@localhost ~]# vim /home/lihui/redis-2.10.3/redis/client.py
class StrictRedis(object):
    """
    Implementation of the Redis protocol.

    This abstract class provides a Python interface to all Redis commands
    and an implementation of the Redis protocol.

可见StrictRedis是封装了所有命令行上操作redis的命令的类,所以才能调用一切数据库操作方法

发表回复