Team-up operations
Copy codeThe Code is as follows: <? Php
$ Redis = new Redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
While (True ){
Try {
$ Value = 'value _ '. date ('Y-m-d H: I: s ');
$ Redis-> LPUSH ('key1', $ value );
Sleep (rand () % 3 );
Echo $ value. "\ n ";
} Catch (Exception $ e ){
Echo $ e-> getMessage (). "\ n ";
}
}
?>
Team-out operationsCopy codeThe Code is as follows: <? Php
$ Redis = new Redis ();
$ Redis-> pconnect ('2017. 0.0.1 ', 127 );
While (True ){
Try {
Echo $ redis-> LPOP ('key1'). "\ n ";
} Catch (Exception $ e ){
Echo $ e-> getMessage (). "\ n ";
}
Sleep (rand () % 3 );
}?>
How to Use Redis for queue operations
Reids is a relatively advanced open-source key-value storage system, which is implemented using ansi c. Similar to memcached, but supports persistent data storage, value supports multiple types: string (same as value in memcached), list, Set, OrderSet, and Hash. All value types support atomic operations, such as appending pop-up elements in the list and inserting and removing elements in the set. Most of the Rdids data is stored in the memory, and its Read and Write efficiency is very high. It provides two persistence Methods: AOF (append operation record file) and DUMP (regular data backup. Redis supports a custom VM (Virtual Memory) mechanism. When the data capacity exceeds the memory, some values can be stored in files. Redis also supports the Master-Slave mechanism for data replication.
Redis list structure can be used as a queue.
From the above Redis scenarios and functions, for our current development activities, we can introduce Redis in those scenarios, rather than turning such a good stuff into "in order to use Redis, what about the tragic situation of Redis? Of course, it is really important to analyze specific issues.
Cache? Distributed cache?
Queue? Distributed Queue?
Some system applications (such as telecommunications, banking, and large-scale Internet applications) will be used. Of course, the popular memcache is a good proof. But in one aspect, whether memcache can include two of them, and it can be better (no practical application, so it just throws ). But from Redis, I can feel that Redis can include both the queue and the cache, and will not cause any troubles in the concurrent environment, because all operations in Redis are atomic operations.
As for comment on the two, the good or the bad will be free, the existence is the reason, the best choice is the best.
Let's start to play with the queue (distributed) design YY in Redis. Please give me some advice.
Scenario:
Currently, all projects are deployed on multiple servers or multiple IP addresses, and the frontend is distributed through F5. Therefore, it is uncertain whether your requests are on that server. The second-kill design in the project did not take this deployment into account at the beginning, and it was also the easiest way to deal with it. It directly locks Row Records for database tables (on Oracle ). It can be said that for different application deployment, and only one database server, this concurrency problem is easily solved. So now let's take a look at whether to move it to the application to avoid adding database servers to the business.
For example, there are two application servers and one database server. The idea is to deploy Redis on the database server. When the two servers operate on the concurrent cache or queue, they first obtain the proxy objects in the two application servers from the Redis server, then perform the column-out operation.
View code implementation (PHP)
Input Queue operation file list_push.phpCopy codeThe Code is as follows: <? Php
$ Redis = getRedisInstance (); // get the Redis instance from the redis Server
$ Redis-> connect ('redis server ip', 6379 );
While (true ){
$ Redis-> lPush ('list1', 'A _ '. date ('Y-m-d H: I: s '));
Sleep (rand () % 3 );
}
?>
Run # php list_push.php &
Output queue operation list_pop.php FileCopy codeThe Code is as follows: <? Php
$ Redis = getRedisInstance (); // get the Redis instance from the redis Server
$ Redis-> pconnect ('redis server ip', 6379 );
While (true ){
Try {
Var_export ($ redis-> blPop ('list1', 10 ));
} Catch (Exception $ e ){
// Echo $ e;
}
}
Implementation Method (Python)
1. write. py)Copy codeThe Code is as follows :#! /Usr/bin/env python
Import time
From redis import Redis
Redis = Redis (host = '2017. 0.0.1 ', port = 127)
While True:
Now = time. strftime ("% Y/% m/% d % H: % M: % S ")
Redis. lpush ('test _ queue ', now)
Time. sleep (1)
2. read. py)Copy codeThe Code is as follows :#! /Usr/bin/env python
Import sys
From redis import Redis
Redis = Redis (host = '2017. 0.0.1 ', port = 127)
While True:
Res = redis. rpop ('test _ queue ')
If res = None:
Pass
Else:
Print str (res)
During the operation, note that the operation is the same list object.
Well, the main idea is almost like this, but there will be discrepancies in real scenarios.