Introduction to "python" Redis and simple use

Source: Internet
Author: User
Tags redis server

first, Redis

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), zset (sorted set-ordered collection), and hash (hash type). These data types support push/pop, add/remove, and Intersection-set and difference sets, and richer operations, and these operations are ATOMIC. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure EFFICIENCY. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (master-slave) synchronization is implemented on this Basis.

Redis is a high-performance key-value database. The emergence of redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational Database. It provides the python,ruby,erlang,php client, which is easy to use and Redis supports Master-slave Synchronization. Data can be synchronized from the primary server to any number of slave servers, from the server to the primary server that is associated with other slave servers. This enables Redis to perform single-layer tree Replication. The data can be written intentionally or unintentionally from the Disk. Because of the full implementation of the Publish/subscribe mechanism, you can subscribe to a channel and receive a complete message release record from the master server when the tree is synchronized anywhere from the Database.

second, python operation Redis

1. Connection mode

Redis-py provides two classes of Redis and Strictredis commands for implementing redis, Strictredis is used to implement most of the official commands, and using the official syntax and commands, Redis is the subclass of Strictredis

1234567 #!/usr/bin/env python #-*-coding:utf- 8 -*- import redis  r = Redis. Redis (host= ' 192.168.0.110 ' , port= 6379 ,db= 0 " r.set ( ' name ' , ' Zhangsan ' )    #添加 print (r.get ( ' 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.

12345678 #!/usr/bin/env python #-*-coding:utf- 8 -*- import redis  pool = Redis. ConnectionPool (host= ' 192.168.0.110 ' port= 6379 ) r = Redis. Redis (connection_pool=pool) r.set ( ' name ' ' Zhangsan ' )    #添加 print (r.get ( ' name '

3. Publish and subscribe

First define a redishelper class, connect redis, define the channel as monitor, define the publish (publish) and subscribe (subscribe) methods.

123456789101112131415161718 #!/usr/bin/env python#-*- coding:utf-8 -*-import redisclass RedisHelper(object):    def __init__(self):        self.__conn = redis.Redis(host=‘192.168.0.110‘,port=6379)#连接Redis        self.channel = ‘monitor‘ #定义名称    def publish(self,msg):#定义发布方法        self.__conn.publish(self.channel,msg)        return True    def subscribe(self):#定义订阅方法        pub = self.__conn.pubsub()        pub.subscribe(self.channel)        pub.parse_response()        return pub

Published by

1234567 #!/usr/bin/env python# -*- coding:utf-8-*-#发布from RedisHelper import RedisHelperobj = RedisHelper()obj.publish(‘hello‘)#发布

Subscribed by

1234567891011 #!/usr/bin/env python# -*- coding:utf-8-*-#订阅from RedisHelper import RedisHelper obj = RedisHelper()redis_sub = obj.subscribe()#调用订阅方法 whileTrue:    msg= redis_sub.parse_response()    print (msg)

Introduction to "python" Redis and simple use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.