This article example tells about Python redis operation. Shared for your reference, as follows:
I. redis
redis is a key-value storage system. Similar to Memcached, it supports relatively more value types for storage, including string, list, set, zset and hash. All of these data types support push/pop, add/remove and fetch intersection and union and difference and richer operations, and all of these operations are atomic. On top of that, redis supports sorting in a variety of different ways. Like memcached, data is cached in memory for efficiency. The difference is that redis periodically writes updated data to disk or writes modification operations to an appended record file and implements master-slave synchronization on top of that.
Redis is a high-performance key-value database. The emergence of redis, to a large extent, to compensate for the memcached such key/value storage shortcomings, in some cases can play a good complementary role to the relational database. It provides Python, Ruby, Erlang, PHP client, easy to use, Redis supports master-slave synchronization. Data can be synchronized from the master server to any number of slave servers , the slave server can be associated with other slave servers of the master server . This allows Redis to perform single-tier tree replication. Slaves can write to data intentionally or unintentionally. The full implementation of the publish/subscribe mechanism makes it possible for a slave database to subscribe to a channel and receive a complete record of the master server's message releases when synchronizing the tree anywhere.
Second, python operation redis
1、Connection method
redis-py provides two classes Redis and StrictRedis for implementing Redis commands, StrictRedis is used to implement most of the official commands and use the official syntax and commands, Redis is a subclass of StrictRedis
#!/usr/bin/env python # -*- coding:utf-8 -*- import redis r = (host='192.168.0.110', port=6379,db=0) ('name', 'zhangsan') #add print (('name')) # Get
2、Connection Pool
redis-py uses a connection pool to manage all connections to a redis server, avoiding the overhead of establishing and releasing connections each time. By default, each Redis instance maintains a connection pool of its own. It is possible to create a connection pool directly, and then as a parameter Redis, so that multiple Redis instances can share a connection pool.
#!/usr/bin/env python # -*- coding:utf-8 -*- import redis pool = (host='192.168.0.110', port=6379) r = (connection_pool=pool) ('name', 'zhangsan') #add print (('name')) # Get
3、Operation
Detailed redis commands
4. Piping
redis-py defaults to creating (pooling connections) and disconnecting (returning connections to the pool) one connection per request. If you want to specify multiple commands in a single request, you can use pipline to specify multiple commands in a single request, and by default, a pipline is an atomic operation.
#!/usr/bin/env python # -*- coding:utf-8 -*- import redis pool = (host='192.168.0.110', port=6379) r = (connection_pool=pool) pipe = (transaction=True) ('name', 'zhangsan') ('name', 'lisi') ()
5. Publish and subscribe
First define a RedisHelper class, connect to Redis, define the channel as monitor, define publish and subscribe methods.
#!/usr/bin/env python #-*- coding:utf-8 -*- import redis class RedisHelper(object): def __init__(self): self.__conn = (host='192.168.0.110',port=6379)#Connect to Redis = 'monitor' #Define the name def publish(self,msg):# Define release methods self.__conn.publish(,msg) return True def subscribe(self):#Define the subscription method pub = self.__conn.pubsub() () pub.parse_response() return pub
publisher
#!/usr/bin/env python # -*- coding:utf-8 -*- #Release from RedisHelper import RedisHelper obj = RedisHelper() ('hello')#Release
subscriber
#!/usr/bin/env python # -*- coding:utf-8 -*- #Subscription from RedisHelper import RedisHelper obj = RedisHelper() redis_sub = ()# Call the subscribe method while True: msg= redis_sub.parse_response() print (msg)
Readers interested in more Python related content can check out this site's topic: theSummary of common database manipulation techniques in Python》、《Summary of Python coding manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
I hope that what I have said in this article will help you in Python programming.