Python operation Redis, Memcache, RabbitMQ, SQLAlchemy
Introduction to Redis:
Redis is an open-source, Advanced Key-value store, which is often referred to as a data structure server because keys can contain string (string), hash (hash), list (linked list), set (set), and Zset (ordered collections) that support push /pop, Add/remove, and overlapping and more extensive operations, Redis supports sorting in a variety of different ways. To ensure efficiency, the data is cached in memory, and it can periodically write updated data to disk or write modifications to the appended record file. Master-slave (Master-Slave) synchronization is implemented on this basis.
Redis installation and basic use:
wget http://download.redis.io/releases/redis-3.0.6.tar.gztar xzf redis-3.0.6.tar.gzcd redis-3.0.6makemake install# Make Prefix=/usr/local/redis install #这种可以直接指定redis安装目录cd src mkdir-p/usr/local/redis/bin cp redis-server REDIS-CLI Redis-benchmark redis-check-aof redischeckdump/usr/local/redis/bin/mkdir-p/usr/local/redis/etc# will be in the source of the redis.conf Copy to/usr/local/redis/etc/redis-3.0.6]# CP redis.conf/usr/local/redis/etc/
Start the service side
/usr/local/redis/bin/redis-server #后台启动加 &
Start the client
/usr/local/redis/bin/redis-cli
Python Operation Redis
Install API
Pip install Redis or source code installation wget--no-check-certificate https://pypi.python.org/packages/source/r/redis/ REDIS-2.8.0.TAR.GZTAR-ZVXF redis-2.8.0.tar.gzmv redis-2.8.0 python-redis-2.8.0cd Python-redis-2.8.0python setup.py Install
1. Operating mode:
Redis-py provides two classes of Redis and Strictredis for implementing Redis commands, Strictredis is used to implement most of the official commands, and using official syntax and commands, Redis is a subclass of Strictredis, Used for backwards compatibility with older versions of Redis-py.
#!/usr/bin/env python#-*-coding:utf-8-*- Import REDISR = Redis. Redis (host= ' 127.0.0.1 ', port=6379) #连接redis服务器, Port is 6379r.set (' name ', ' Saneri ') #创建一个键值对print r.get (' name ') # Print the value of the key value pair name
2. 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.
#!/usr/bin/env python#-*-coding:utf-8-*-import Redis pool = Redis. ConnectionPool (host= ' 127.0.0.1 ', port=6379) r = Redis. Redis (Connection_pool=pool) r.set (' Name ', ' Rain ') print r.get (' name ')
classConnectionPool (object): ... ..def __init__(Self, connection_class=connection, max_connections=None,**connection_kwargs):#calling constructors when class initializationMax_connections = Max_connectionsor2 * * 31if notIsinstance (max_connections, (int, long))orMax_connections < 0:#determine if the input max_connections is legal RaiseValueError ('"Max_connections" must be a positive integer') Self.connection_class= Connection_class#set the corresponding parametersSelf.connection_kwargs =Connection_kwargs self.max_connections=max_connections self.reset ()#Reset operation when initializing the ConnectionPool defReset (self): Self.pid=os.getpid () self._created_connections= 0#The counters for the connections that have been createdSelf._available_connections = []#declares an empty array to hold the available connectionsSelf._in_use_connections = set ()#declares an empty collection to hold a connection that is already in useSelf._check_lock =Threading. Lock () ....defGet_connection (self, command_name, *keys, **options):#How to get a connection in a connection pool "Get A connection from the pool"self._checkpid ()Try: Connection= Self._available_connections.pop ()#gets and deletes the element that represents the connection, the first time it gets connectiong, because _available_connections is an empty array,The Make_connection method is called directlyexceptindexerror:connection=self.make_connection () self._in_use_connections.add (connection)#adds an element to the collection that represents the connection being used returnConnectiondefMake_connection (self):#gets the method of the connection call when the _available_connections array is empty "Create a new connection" ifSelf._created_connections >= self.max_connections:#determines whether the created connection has reached the maximum limit, max_connections can be initialized with parameters RaiseConnectionerror ("Too Many connections") Self._created_connections+ = 1#The value +1 that represents the connection that was created returnSelf.connection_class (**self.connection_kwargs)#returns a valid connection with the default of connection (**self.connection_kwargs) defRelease (self, Connection):#release connection, link is not broken, just exists in the link pool "releases the connection back to the pool"self._checkpid ()ifConnection.pid! =Self.pid:returnSelf._in_use_connections.remove (Connection)#remove an element from the collectionSelf._available_connections.append (Connection)#and added to the array of _available_connections defDisconnect (self):#Disconnect links from all connection pools "Disconnects all connections in the pool"All_conns=Chain (self._available_connections, self._in_use_connections) forConnectioninchAll_conns:connection.disconnect ()
ConnectionPool class
3. Pipeline
Redis-py The default is to create each request (Connection pool request connection) and disconnect (return connection pool) One connection operation, if you want to specify more than one command in a single request, you can use Pipline to implement a single request to specify multiple commands, and by default Pipline is an atomic operation.
#!/usr/bin/env python#-*-coding:utf-8-*-import Redis pool = Redis. ConnectionPool (host= ' 127.0.0.1 ', port=6379) #建立连接, manage with pool to avoid disconnecting r = Redis. Redis (Connection_pool=pool) #redis共享连接池 # pipe = r.pipeline (transaction=false) pipe = R.pipeline (transaction=true #连接池申请连接r. Set (' Name ', ' Lisi ') #插入键值对r. Set (' Role ', ' Male ') Pipe.execute () #申请断开
Publish Subscription Model:
#!/usr/bin/env python#-*-coding:utf-8-*-ImportRedisclassRedishelper:def __init__(self): self.__conn= Redis. Redis (host='127.0.0.1')#Connect to a Redis serverSelf.chan_sub ='fm104.5' #Subscribe to ChannelsSelf.chan_pub ='fm104.5' #Publish Channel defPublic (self, msg):#define publish function, MSG for post messageSelf.__conn. Publish (Self.chan_pub, msg)returnTruedefSubscribe (self): Pub= self.__conn. PubSub ()#define a receive function, receive a messagepub.subscribe (self.chan_sub) pub.parse_response ()#Waiting for Messages returnPub
Redis_demo
Subscribers:
#!/usr/bin/env python#-*-coding:utf-8-*-from redis_demo import redishelperobj = Redishelper () #实例化对象redis_sub = ob J.subscribe () while True: msg= redis_sub.parse_response () print msg
Published by:
#!/usr/bin/env python#-*-coding:utf-8-*-from redis_demo import redishelperobj = Redishelper () obj.public (' Hello ')
See also: https://github.com/andymccurdy/redis-py
Python operation Redis, Memcache, RabbitMQ, SQLAlchemy