Example of redis queue operation of php

  • 2020-05-16 06:33:47
  • OfStack

The operation of entrance

 
<?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"; 
} 
} 
?> 

The team 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 do I use Redis for queue operations
Reids is a relatively advanced open source key-value storage system implemented by ANSI C. It is similar to memcached, but supports persistent data storage, and value supports a variety of types: strings (value in memcached), lists, collections (Set), ordered collections (OrderSet), and Hash. All value types support atomic operations, such as appending a pop-up element to a list, inserting a remove element into a collection, and so on. Most of Rdids's data is in memory, which makes it very efficient to read and write. It provides two persistence methods, AOF (append operation record file) and DUMP (regular data backup). Redis supports a custom VM (virtual memory) mechanism that allows you to store part of Value in a file when data capacity exceeds memory. At the same time, Redis supports the Master-Slave mechanism for data replication.
You can use Redis's list structure as a queue.
In terms of the above Redis scenarios and functions, how can we introduce Redis into those scenarios for our current development activities, instead of turning something so good into a tragic situation of "Redis for Redis"? Of course, it's a case by case basis, and that's really important.
The cache? Distributed cache?
The queue? Distributed queues?
Certain system applications (e.g., telecommunications, banking, and large Internet applications) can be used, as evidenced by the current popularity of memcache. But in a way, does memcache do a better job of including both sheets (no real application, so just throwing)? But from Redis, I get the sense that Redis is going to be able to include both the queue and the cache, and neither of them is going to be a problem in the concurrency environment, because the operations in Redis are atomic.
As for commenting on which is better or worse, it is not necessary to do so. Being is the reason, and choosing what is right is the best.
Let's play with YY queue (distributed) design in Redis.
Situation scenario:
The current projects are all deployed on multiple servers or multiple IP, and the front desk is distributed through F5, so it is impossible to determine which server the user's request falls on. For the project, there is a one-second kill design, which does not take this deployment into account at first, but also USES the easiest way to handle, locking row records directly to database tables (on Oracle). In other words, for different application deployments with only one database server, this concurrency problem is "easily" solved. So now consider 1, whether to move to the application, to avoid the database server also mixed with the business.
For example, there are now two application servers and one database server. The idea is to deploy Redis on a database server, and when two servers are operating concurrent caches or queues, first get proxy objects from the Redis server on both application servers, and then do the in-and-out operation.
Look at the code implementation (PHP)
Enqueued operation file list_push.php
 
<?php 
$redis = getRedisInstance();// from Redis The server gets redis The instance  
$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 the queue operation list_pop.php file
 
<?php 
$redis = getRedisInstance();// from Redis The server gets redis The instance  
$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. Enqueue (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. Exit 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 manipulating, note that you are manipulating the same list object.
Oh, now the main idea is almost so, but in the real scene, there will be differences.


Related articles: