Redis and memcached are similarly an open-source memory storage system, and the storage format is also a key-value pair. However, compared with memcached, memcached's value only supports strings, while Redis supports strings, lists, collections, hashes, and so on, and these data types support operations such as Push/pop,add/remove, and are all atomic in nature. That is, like a database of things, all operations are either completed or all fail, and then rolled back to the previous state.
Now let's look at his simple use and the ability to publish subscriptions.
Server Installation
[Email protected] ~]# wget http://download.redis.io/releases/redis-3.0.6.tar.gz [[email protected] ~]# tar Xzf Redis-3.0.6.tar.gz[[email protected] ~]# CD Redis-3.0.6/[[email protected] redis-3.0.6]# make
The installed interface is as follows, using the default profile, his access port is 6379
make[1]: leaving directory '/root/redis-3.0.6/src ' [[email protected] redis-3.0.6]# src/redis-server24344:c 07 nov 10:40:21.763 # warning: no config file specified, using the default confif24344:M 07 Nov 10:40:21.764 * increased maximum number of open files to 10032 (it was or. _._ _.-' __ '-._ _.-' ' . ' _. '-._ Redis 3.0.6 (00000000/0) 64 bit .-' .-' . ' \/ _.,_ '-._ ( ' , .-' | ', running in standalone mode | '-._ '-...-' __...-. '-._| ' ' _.-' | port: 6379 | '-._ '. _ / _.-' | PID: 24344 '-._ '-._ '-./ _.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' | | '-._ '-._ _.-' _.-' | http://redis.io '-._ '-._ '-.__.-' _.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' | |&NBsp; '-._ '-._ _.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' '-._ '-.__.-' _.-' '-._ _.-' '-.__.-' 24344:m 07 nov 10:40:21.766 # WARNING: The TCP backlog setting of 511 cannot be Enforced.24344:m 07 nov 10:40:21.766 # server started, redis version 3.0.624344:M 07 Nov 10:40:21.766 # WARNING overcommit_memory is set to 0! background save may.24344:m 07 nov 10:40:21.766 # Warning you have transparent huge pages (THP) support enab.24344:M 07 Nov 10:40:21.766 * the server is now ready to accept connections on port 6379
And don't forget to open the firewall port
[Email protected] ~]# firewall-cmd--add-port=6379/tcp--permanentsuccess[[email protected] ~]# systemctl restart Firewalld
Next use the same directory of the client program test, successfully set up and get data
[[email protected] redis-3.0.6]# src/redis-cli127.0.0.1:6379> set foo barok127.0.0.1:6379> get foo "bar" 127.0.0.1:6379>
Now look at how Python is called under.
First install the Redis module
C:\windows\system32>pip Install rediscollecting redis Using cached redis-2.10.5-py2.py3-none-any.whlinstalling Collected packages:redissuccessfully installed redis-2.10.5
A first example:
Import Redisr=redis. Redis (host= ' Sydnagios ', port=6379) r.set (' name ', ' John ') print (R.get (' name '))-------B ' John '
In addition to connecting directly to a connection pool, each Redis instance has its own pool of connections, which allows multiple instances to share a single connection pool
Import Redispool=redis. ConnectionPool (host= ' Sydnagios ', port=6379) R=redis. Redis (connection_pool=pool) print (r.get (' name '))
Redis and memcached, because he supports a variety of data structures, so the corresponding operation function is also many, almost the memcached 10 times times
Here are some common, specific functions that can be used to refer to http://www.cnblogs.com/wupeiqi/articles/5132791.html
Batch setup, bulk acquisition
Pool=redis. ConnectionPool (host= ' Sydnagios ', port=6379) R=redis. Redis (Connection_pool=pool) r.mset (name= ' Kevin ', age=14) print (r.mget (' name ', ' age ')-----------[b ' Kevin ', B ' 14 ']
Self-increment
Import Redispool=redis. ConnectionPool (host= ' Sydnagios ', port=6379) R=redis. Redis (Connection_pool=pool) R.incrby (' age ', ten) print (R.get (' age '))-------B ' 24 '
Delete
Import Redisr=redis. Redis (host= ' Sydnagios ', port=6379) r.delete (' Set1 ')
Hash Batch Operation
Import Redispool=redis. ConnectionPool (host= ' Sydnagios ', port=6379) R=redis. Redis (Connection_pool=pool) r.hmset (' computer ', {' Macbook ': 20000, ' Surface3 ': ", ' IPhone7 ': 9000}) Print (R.hmget (' Computer ', ' Macbook ')--------[b ' 20000 ']
List operation
Import Redisr=redis. Redis (host= ' Sydnagios ', port=6379) r.lpush (' List1 ', ' apple ') r.lpush (' list1 ', ' pear ') print (R.llen (' List1 ')) print ( R.lpop (' List1 ')) print (R.llen (' List1 '))------2b ' pear ' 1
Set operation, the elements of the collection cannot be duplicated
Import Redisr=redis. Redis (host= ' Sydnagios ', port=6379) r.sadd (' Set1 ', ' orange ') r.sadd (' Set1 ', ' Mango ') print (R.scard (' Set1 ')) print ( R.smembers (' Set1 '))---------2{b ' Mango ', B ' orange '}
Pipeline
The default redis-py will automatically connect and then disconnect when the operation request is executed, and we can pass through the pipeline and execute the multiple operations at once.
#!/usr/bin/env python#-*-coding:utf-8-*-import redispool = Redis. ConnectionPool (host= ' Sydnagios ', port=6379) R = Redis. Redis (connection_pool=pool) # pipe = R.pipeline (transaction=false) pipe = R.pipeline (transaction=true) r.set (' name ', ' Alex ') R.set (' age ', +) Pipe.execute ()
Because there are so many functions of redis, it is not mentioned here.
Now let's look at a Redis usage scenario, publish and subscribe.
Simply put, the publisher can post data to a channel, and then anyone who subscribes to the channel can receive the message.
s3.py
Import Redisclass redishelper:def __init__ (self): Self.__conn = Redis. Redis (host= ' Sydnagios ') def publish (self, MSG, chan): Self.__conn.publish (Chan, msg) return True def s Ubscribe (Self, chan): pub = Self.__conn.pubsub () pub.subscribe (chan) pub.parse_response () Retu RN Pub
s4.py (subscribers)
Import s3obj = s3. Redishelper () data = Obj.subscribe (' fm111.7 ') print (Data.parse_response ())
s5.py (publisher)
Import s3obj = s3. Redishelper () obj.publish (' Alex db ', ' fm111.7 ')
The subscriber's code is executed first, the waiting state is executed, and then the publisher's program is sent to the specified channel to send the data ' Alex DB ', and the Subscriber receives the following data
[B ' message ', B ' fm111.7 ', B ' Alex DB ']
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1870139
Python Learning Notes-Redis