由于学习开发监控软件的需要,因此需要使用到Redis,这里简单介绍。
1.安装
可以查看这里的文章:http://www.linuxidc.com/Linux/2014-05/101544.htm
2.启动
由于采用的是源码安装的方式,所以直接进入src目录,启动redis-server:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis-2.8.9/src$ ./redis-server [12681] 16 Oct 00:06:52.964 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf [12681] 16 Oct 00:06:52.967 # You requested maxclients of 10000 requiring at least 10032 max file descriptors. [12681] 16 Oct 00:06:52.968 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted. [12681] 16 Oct 00:06:52.968 # Current maximum open files is 1024. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'. _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.8.9 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 12681 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [12681] 16 Oct 00:06:52.974 # Server started, Redis version 2.8.9 [12681] 16 Oct 00:06:52.974 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. [12681] 16 Oct 00:06:52.976 * DB loaded from disk: 0.002 seconds [12681] 16 Oct 00:06:52.977 * The server is now ready to accept connections on port 6379
出现上面所示的提示,说明已经正常启动了redis。
3.交互式操作
进入src目录,运行redis-cli即可进入redis交互界面:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis-2.8.9/src$ ./redis-cli 127.0.0.1:6379>
基本操作:
#查看帮助 127.0.0.1:6379> help set SET key value [EX seconds] [PX milliseconds] [NX|XX] summary: Set the string value of a key since: 1.0.0 group: string #创建key-value 127.0.0.1:6379> set name xpleaf OK #获得key对应的value 127.0.0.1:6379> get name "xpleaf" #创建有时间的key-value 127.0.0.1:6379> set name2 CL ex 5 OK #创建列表 127.0.0.1:6379> lpush stu_list xpleaf yonghaoye CL (integer) 3 127.0.0.1:6379> lpush stu_list CLYYH (integer) 4 #获取列表内容 127.0.0.1:6379> lrange stu_list 1 4 1) "CL" 2) "yonghaoye" 3) "xpleaf" 127.0.0.1:6379> lrange stu_list 0 4 1) "CLYYH" 2) "CL" 3) "yonghaoye" 4) "xpleaf" #删除key-value或其它数据类型 127.0.0.1:6379> del name (integer) 1
3.在Python交互器中使用redis
要使用Python来操作Redistribute,则需要安装Python与Redis通信的接口:
apt-get install python-redis
创建一个连接redis数据库的py文件,程序代码如下:
#!/usr/bin/env python import redis r = redis.Redis(host='localhost',port=6379,db=0)
在交互器中连接Redis数据库:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7$ python Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import tab,redis_conn3
基本操作:
#查看所有的key >>> redis_conn3.r.keys() ['YourKey', 'stu_list', 'k1', 'k3'] #创建key-value >>> redis_conn3.r.set('k1', 'v1') True #获取key所对应的value >>> redis_conn3.r['k2'] 'v2' 或 >>> redis_conn3.r.get('k2') 'v2' #保存Python中的字典到Redis数据库中 >>> redis_conn3.r['Py_dict'] = json.dumps(a) >>> redis_conn3.r['Py_dict'] '{"key": "value"}' #取出保存在Redis数据库中的Python字典 >>> b = json.loads(redis_conn3.r['Py_dict']) >>> b {u'key': u'value'} >>> print b {u'key': u'value'} >>> b['key'] u'value'