Fetch the repository succeeded.
This action will force synchronization from 前沿网络科技工作室/EasyTask, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
<?php
namespace EasyTask;
/**
* Class Queue
* @package EasyTask
*/
class Queue
{
/**
* 进程锁
* @var Lock
*/
private $lock;
/**
* 队列文件
* @var string
*/
private $queFile;
/**
* 构造函数
* @param string $name
* @throws
*/
public function __construct($name = 'queue')
{
// 创建进程锁
$this->lock = new Lock($name);
// 创建队列文件
$path = Helper::getQuePath();
$file = $path . '%s.dat';
$this->queFile = sprintf($file, md5($name));
if (!file_exists($this->queFile)) {
if (!file_put_contents($this->queFile, '[]', LOCK_EX)) {
Helper::showError('crate queFile failed,please try again');
}
}
}
/**
* 向队列投递数据
* @param string $item
*/
public function push($item)
{
$this->lock->execute(
function () use ($item) {
// read
$content = file_get_contents($this->queFile);
$queue_data = $content ? json_decode($content, true) : [];
$queue_data = is_array($queue_data) ? $queue_data : [];
// write
array_push($queue_data, $item);
if (!file_put_contents($this->queFile, json_encode($queue_data))) {
Helper::showError('failed to save data to queue file');
}
}
);
}
/**
* 从队列弹出数据
* @return string|null
*/
public function shift()
{
return $this->lock->execute(
function () {
// read
$content = file_get_contents($this->queFile);
$queue_data = $content ? json_decode($content, true) : [];
$queue_data = is_array($queue_data) ? $queue_data : [];
// shift+write
$value = array_shift($queue_data);
if (!file_put_contents($this->queFile, json_encode($queue_data))) {
Helper::showError('failed to save data to queue file');
}
return $value;
}
);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。