Example of PHP operating Redis queue

Keywords: PHP Redis memcached

IDS is an advanced open source key value storage system, which is implemented in ANSI C. It is similar to memcached, but supports persistent data storage

Queue operation:

<?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 of line operation:

<?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 for queue operation
IDS is an advanced open source key Value storage system, which is implemented in ANSI C. It is similar to memcached, but supports persistent data storage. At the same time, Value supports many types: string (the same as Value in memcached), list, set, orderset and Hash. All Value types support atomic operations, such as adding pop-up elements in the list, inserting and removing elements in the collection, etc. Most of the data of Rdids is located in memory, and its reading and writing efficiency is very high. It provides two persistence modes: AOF (additional operation record file) and DUMP (regular data backup). Redis supports a customized VM (virtual memory) mechanism. When the data capacity exceeds the memory, some values can be stored in files. At the same time, redis supports the master slave mechanism for data replication.
Redis's list structure can be used as a queue
From the above scenarios and functions of Redis, for our current development activities, can we introduce Redis into those scenarios instead of turning such a good thing into a tragic situation of "Redis for the use of Redis"? Of course, specific analysis of specific problems is really important.
Cache? Distributed cache?
Queue? Distributed queue?
Some system applications (such as telecommunications, banking and large Internet Applications) will be used. Of course, memcache, which is now popular, is a good proof; But from a certain point of view, whether memcache can include two of them and do better (it has not been actually applied, so it is just thrown). But from Redis, I can feel that Redis can include both queue and cache, and will not cause problems in the concurrent environment, because the operations in Redis are atomic operations.
As for commenting on which is better or worse, existence is the reason, and choosing the appropriate is the best.
Let's start to play YY, the queue (distributed) design in Redis. Please give us more advice.
Situation scenario:
Current projects are deployed on multiple servers or multiple IP S, and the foreground is distributed through F5, so it is uncertain which server the user's request falls on. There is a second kill design in the project. At the beginning, this deployment was not considered. At the same time, it is also the easiest way to lock row records directly to the database table (on Oracle). It can be said that for different application deployments and only one database server, this concurrency problem is "easily" solved. So now consider whether to move to the application to avoid the database server being mixed with the business.
For example, there are two application servers and one database server. The idea is to deploy Redis on the database server. When operating concurrent cache or queue, the two servers first obtain the proxy objects on the two application servers from the Redis server, and then do the listing operation.
Look at the code implementation (PHP)
Queue operation file list_push.php

<?php 
$redis = getRedisInstance();//Get the redis instance from the redis server 
$redis->connect('Redis The 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 of queue operation list_pop.php file

<?php 
$redis = getRedisInstance();//Get the redis instance from the redis server 
$redis->pconnect('Redis The server IP', 6379); 
while(true) { 
try { 
var_export( $redis->blPop('list1', 10) ); 
} catch(Exception $e) { 
//echo $e; 
} 
} 

Implementation method (Python)
1. Queue (write.py)

#!/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 of queue (read.py)

#!/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 operating, note that the same list object is to be operated.

Posted by dazzathedrummer on Sat, 20 Nov 2021 01:40:39 -0800