当前位置:网站首页>Redis cache message queue
Redis cache message queue
2022-06-26 04:21:00 【Scholar and】
// Message queue class :
// 1: Definition :
// queue , Team head , A party , Maximum
// 2: Define methods
// Initialize queue , Judge team empty , Judge that the team is full , The team , Out of the team
// 【 Judge if the team is full , The team is full : Return to wait ; The team is not full : Queued write data to cache queue , And return the success value ;】
// 3: Timing task , Loop handler :
// 【 Judge whether the team is empty , Team space : Don't deal with ; No team empty : Circular queue , Processing data , Write to database , Out of the team , Delete the current queue value ;】
class redisList{
public $redis;
// Maximum queue length
public $max_size;
// Queue key value
public $list_key;
// Database instance
public $db;
public function __construct(){
// Instantiation redis cache
$this->redis = new Redis();
$this->redis->pconnect('127.0.0.1', 6379);
// Queue maximum
$this->max_size = 100000;
$this->list_key = 'list';
$this->db = DB::getIntance();
}
// Queue request ( Call queue , Fast write cache , And give the return value )
public function intoQueue($data){
// Current number of queue elements
$list_num = $this->redis->lLen( $this->list_key );
// If the current number of queues , Less than the queue maximum , You are allowed to join the team , Otherwise return to wait or end
if ( $list_num < $this->max_size ) {
// Write data to the end of the queue
$this->redis->rPush($this->list_key, $data);
echo " Team success ";
} else {
echo ' The queue is full , Please be patient ';
}
}
// Out of the team request
public function outQueue() {
// Get the first element of the queue , And delete it from the queue
$data = $this->redis->lPop( $this->list_key );
$return = $data ? $data : false;
return $return;
}
// Timing task , Business processing
public function businessHandle(){
// Current number of queue elements
$list_num = $this->redis->lLen( $this->list_key );
// If the team is empty , Do not perform
if ($list_num == 0) {
return false;
}
while(1) {
try {
$data = $this->outQueue();
if ($data) {
/** Logic and data processing */
// Data processing failed , Re write data to the queue
//$this->rPush($this->list_key, $data);
} else {
break;
}
} catch (Exception $e) {
// Record error messages
echo $e->getMessage();
}
}
}
public function __destruct(){
$this->redis->close();
}
}
边栏推荐
- How much do you make by writing a technical book? To tell you the truth, 2million is easy!
- In the matter of getting customers at sea, how can advertisers play besides digital advertising?
- 一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
- Understand CGI and fastcgi
- What are the advantages and risks of paper gold investment
- Development trend and prospect forecast report of China's financial industry 2022-2028 Edition
- Go SQL parsing time Time type
- redis集群的方式
- ipvs之ipvs0网卡
- Notes on enterprise wechat development [original]
猜你喜欢

The stc-isp burning program for 51 single chip microcomputer always shows that "the target single chip microcomputer is being detected..." the cold start board does not respond

35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling

使用Jsoup提取接口中的图片

Zeromq from getting started to mastering

捕获数据包(Wireshark)

Implementation of seven classes of BlockingQueue interface

线程同步之读写锁
![Laravel framework Alipay payment fails to receive asynchronous callback request [original]](/img/a7/139604ec3a977f2a4e96f392e56602.jpg)
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
![[Qunhui] no port access (reverse proxy + intranet penetration)](/img/bc/b1e0c5c382e30fbcea28fbc68c1151.jpg)
[Qunhui] no port access (reverse proxy + intranet penetration)

Minecraft 1.16.5 biochemical 8 module 1.9 version 1.18 version synchronization
随机推荐
Mobile terminal pull-down loading pull-down loading data
Lua语法讲解
Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am
六、项目实战---识别猫和狗
Lua grammar explanation
[Qunhui] Internet access + custom port
Go SQL parsing time Time type
List of provinces, cities and counties in China
Which is the best embedded visual programming software? (introduction, evaluation and selection of visual programming platform) [scratch, mind+, mixly]
Your requirements could not be resolved
捕获数据包(Wireshark)
MySQL enable logbin in Qunhui docker
What if the serial port fails to open when the SCM uses stc-isp to download software?
Mutex of thread synchronization (mutex)
OSS CDN alicloud configuration method
Add, delete, modify and query curd in PHP native SQL
[Qunhui] no port access (reverse proxy + intranet penetration)
Using jsup to extract images from interfaces
Spark - understand parquet
Gateway can not connect to tcp://127.0.0.1: Connection refused