First, installation Services
Download, compile and install
wget HTTP://DOWNLOAD.REDIS.IO/RELEASES/REDIS-3.0.1.TAR.GZTAR-ZXVF REDIS-3.0.1.TAR.GZCD Redis-3.0.1make
Binary files are compiled after completion in the SRC directory, start the Redis service with the following command:
$ Src/redis-server &
The client can also be installed as follows:
Pip Install Redis
Second, Redis connection example
Redis is stored in the form of Key-value. First we instantiate an object R with the IP and publish port of the host where Redis is located, and then go to set up and remove the get value.
Example:
Import Redisredis_config = {"Host": "192.168.2.230", "port": 6379}r = Redis. Redis (**redis_config) r.set ("name", "Huangzhenping") print (R.keys ()) Print (R.get ("name"))
Operation Result:
Name
Huangzhenping
Third, Connection pool
Redis-py uses connection pool to manage all connections to a Redis server, avoiding the overhead of each establishment and release of the connection. By default, each Redis instance maintains its own pool of connections. You can create a connection pool directly, and then as a parameter Redis, you can implement multiple Redis instances to share a single connection pool
Example:
Import Redisredis_config = {"Host": "192.168.2.230", "port": 6379}pool = Redis. ConnectionPool (**redis_config) R = Redis. Redis (Connection_pool=pool) r.set ("Age", "the") Print (R.get ("Age"))
Operation Result:
27
or wrap the connection pool into a function for easy invocation:
Import Redisdef get_redis_connect (): Redis_config = {"Host": "192.168.2.230", "Port": 6379} P Ool = Redis. ConnectionPool (**redis_config) R = Redis. Redis (Connection_pool=pool) return r if __name__ = = "__main__": r = Get_redis_connect () print (R.keys ())
Iv. Pipelines
Redis-py is created by default on each request (connection pooling request connection) and disconnected (return connection pool) One connection operation, if you want to specify multiple commands in a single request, you can make use Pipline to implement a single request to specify multiple commands, and the next time Pipline is an atomic operation by default. The Redis server finishes processing multiple commands and then packets the results of multiple commands back to the client. It is important to note that Redis must cache all of the command's processing results before all commands are processed, and the more commands are packaged, the more memory is consumed by the cache.
Example: comparing the time spent using pipelines and not using pipe processing
Import redisimport datetimedef withpipe (R): pipe = r.pipeline ( Transaction=true) for i in xrange (1, 1000): key = "Key1_" + str (i) value = "Value1_" + str (i) pipe.set ( Key, value) pipe.execute () def withoutpipe (R): for i in xrange (1, 1000): key = "Key2_" + str (i) value = "Value2_" + str (i) r.set (key, value) redis_config = { "Host": " 192.168.2.230 ", "Port": 6379, "DB": 0}if __name__ == "__main__": pool = redis. ConnectionPool (**redis_config) r1 = redis. Redis (Connection_pool=pool) r2 = redis. Redis (Connection_pool=pool) start = datetime.datetime.now () withpipe (R1) end = datetime.datetime.now () t_time = (End - start) Microseconds print (" WITHPIPE TIME IS: {0} ". Format (t_time)) start = Datetime.datetime.now () withoutpipe (R2) End = datetime.datetime.now () t_time = (End - start). Microseconds print ("Withoutpipe tIME IS: {0} ". Format (T_time))
Operation Result:
Withpipe Time is:17000
Withoutpipe Time is:105000
Python Connect to Redis