As a result of learning to develop monitoring software needs, so need to use to Redis, here is a brief introduction.
1. Installation
You can view the article here: http://www.linuxidc.com/Linux/2014-05/101544.htm
2. Start
Since the use of the source code installation method, so directly into the SRC directory, start redis-server:
[email protected]:/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 '-._ '-._ '-.__.-' _.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' | | '-._ '-._ _.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' '-._ '-.__.-'  &Nbsp; _.-' '-._ _.-' '-.__.-' &nbsP [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
The hints shown above indicate that Redis has been started normally.
3. Interactive operation
Enter the SRC directory and run redis-cli to access the Redis Interactive interface:
[Email protected]:/mnt/hgfs/python/day7/redis-2.8.9/src$ ./redis-cli127.0.0.1:6379>
Basic operation:
#查看帮助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# created key-value127.0.0.1:6379> Set name xpleafok# received Value127.0.0.1:6379> Get Name "Xpleaf" #创建有时间的key-value127.0.0.1:6379> set name2 CL ex 5ok# create list 127.0.0.1 :6379> lpush stu_list xpleaf Yonghaoye CL (integer) 3127.0.0.1:6379> lpush stu_list clyyh (integer) 4# Get list Contents 127.0.0.1:6379> lrange stu_list 1) "CL" 2) "Yonghaoye" 3) "Xpleaf" 127.0.0.1:6379> lrange stu_list 0 "CLYY H "2)" CL "3)" Yonghaoye "4)" Xpleaf "#删除key-value or other data type 127.0.0.1:6379> del name (integer) 1
3. Using Redis in the Python interaction
To use Python to manipulate redistribute, you need to install the interface for Python communication with Redis:
Apt-get Install Python-redis
Create a py file that connects to the Redis database with the following program code:
#!/usr/bin/env pythonimport REDISR = Redis. Redis (host= ' localhost ', port=6379,db=0)
To connect a Redis database in an interactive device:
[Email protected]:/mnt/hgfs/python/day7$ pythonpython 2.7.3 (default, 1, 05:14:39) [GCC 4.6.3] on Linux2type "he LP "," Copyright "," credits "or" license "for more information.>>> import tab,redis_conn3
Basic operation:
#查看所有的key >>> Redis_conn3.r.keys () [' Yourkey ', ' stu_list ', ' K1 ', ' K3 '] #创建key-value>>> redis_ Conn3.r.set (' K1 ', ' v1 ') true# get key corresponding to value>>> redis_conn3.r[' K2 '] ' v2 ' or >>> 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 '
This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1703387
"Python Tour" seventh (ii): Redis usage Basics