Examples of Redis queue operations (PHP)

Source: Internet
Author: User
Tags getmessage memcached pconnect rand redis redis server
Reids is a relatively advanced open source Key-value storage System, with ANSI C implementation. It is similar to memcached, but supports persistent data storage

Join operation
Copy code code as follows:
<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
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 ";
}
}
?>

Out Team operation
Copy code code as follows:
<?php
$redis = new Redis ();
$redis->pconnect (' 127.0.0.1 ', 6379);
while (True) {
try{
echo $redis->lpop (' Key1 '). " \ n ";
}catch (Exception $e) {
echo $e->getmessage (). \ n ";
}
Sleep (rand ()%3);
}?>

How to use Redis to do queue operations
Reids is a relatively advanced open source Key-value storage System, with ANSI C implementation. It is similar to memcached, but supports persistent data storage, while value supports multiple types: strings (value in the same memcached), lists, sets, ordered sets (Orderset), and hashes. All value types support atomic operations, such as appending a pop-up element to the list, inserting a removal element in the collection, and so on. Most of the data in Rdids is in memory, and its read and write efficiency is very high, providing both aof (Append operation record file) and dump (regular data backup) Two kinds of persistence methods. Redis supports a custom VM (virtual memory) mechanism that stores part value in a file when the data capacity exceeds memory. At the same time, Redis supports the master-slave mechanism and can replicate data.
You can use the Redis list structure as a queue.
From the above Redis scene and function, to our current development activity, actually can introduce redis in those scenes, instead of such a good dongdong evolved into "to use the Redis, and Redis" of the tragic situation. Of course, concrete analysis of specific issues, this is really important ha.
Cache. Distributed caching.
Queue. Distributed queues.
Some system applications (for example, telecommunications, banks and large Internet applications, etc.) will be used, of course, now the memcache is very good proof, but in a way, memcache can be included in the two, and can do better (no actual application, so just throw). But from the Redis, I can feel that, Redis, the queue and the cache can be both included, and will not create a concurrent environment, because the operations in Redis are atomic operations.
As to comment on both the good and the bad is free, existence is the reason, choose the right is the best.
The following begins to play Redis in the queue (distributed) design yy bar, please a lot of shrimp advice.
Condition Scenario:
Now the project, are deployed in multiple servers, or multiple IP, and the foreground through the F5 distribution, so the user's request on the server, it is not certain. For a project, there is a second kill design that is not considered for this deployment at the outset, but is also the easiest way to handle the database table lock line Record (Oracle). It can be said that for different application deployment, and only one database server, very "easy" to solve this concurrency problem. So now consider whether to move to the application, to avoid the database server is also doped into the business.
For example, there are now 2 application servers, 1 database servers. The idea is that the Redis is deployed on the database server, and when the two servers operate the concurrency cache or queue, they get the proxy object from the Redis server on the two application servers, and then do the operations that list the columns.
See Code implementation (PHP)
Enter queue operation file list_push.php
Copy code code as follows:
<?php
$redis = Getredisinstance ()//Get Redis instance from Redis server
$redis->connect (' Redis server IP ', 6379);
while (true) {
$redis->lpush (' List1 ', ' A_ '. Date (' y-m-d h:i:s '));
Sleep (rand ()%3);
}
?>

Execute # PHP list_push.php &
Out Queue Operation list_pop.php file
Copy code code as follows:
<?php
$redis = Getredisinstance ()//Get Redis instance from 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. Into the queue (write.py)
Copy code code as follows:
#!/usr/bin/env python
Import time
From Redis import Redis
Redis = Redis (host= ' 127.0.0.1 ', port=6379)
While True:
now = Time.strftime ("%y/%m/%d%h:%m:%s")
Redis.lpush (' Test_queue ', now)
Time.sleep (1)

2. Out Queue (read.py)
Copy code code as follows:
#!/usr/bin/env python
Import Sys
From Redis import Redis
Redis = Redis (host= ' 127.0.0.1 ', port=6379)
While True:
res = Redis.rpop (' Test_queue ')
If res = None:
Pass
Else
Print str (RES)

When you are working, notice that the same list object is being manipulated.
Oh, now the main idea is almost the case, but the real scene, there will be discrepancies.

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.