1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-27 21:10:24 +08:00
koogua 0336a54911 1.源文件增加版权信息
2.群组状态和课程协同
2021-06-13 15:49:47 +08:00

83 lines
1.5 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Services\Logic\FlashSale;
use App\Services\Logic\FlashSaleTrait;
use App\Services\Logic\Service as LogicService;
class Queue extends LogicService
{
use FlashSaleTrait;
public function init($id)
{
$sale = $this->checkFlashSale($id);
if ($sale->stock < 1) return;
$redis = $this->getRedis();
$keyName = $this->getKeyName($id);
$ttl = $sale->end_time - time();
$values = [];
for ($i = 0; $i < $sale->stock; $i++) {
$values[] = 1;
}
$redis->del($keyName);
$redis->lPush($keyName, ...$values);
$redis->expire($keyName, $ttl);
}
public function pop($id)
{
$redis = $this->getRedis();
$keyName = $this->getKeyName($id);
return $redis->lPop($keyName);
}
public function push($id)
{
$redis = $this->getRedis();
$keyName = $this->getKeyName($id);
return $redis->lPush($keyName, 1);
}
public function delete($id)
{
$redis = $this->getRedis();
$keyName = $this->getKeyName($id);
return $redis->del($keyName);
}
public function length($id)
{
$redis = $this->getRedis();
$keyName = $this->getKeyName($id);
return $redis->lLen($keyName);
}
protected function getKeyName($id)
{
return "flash_sale_queue:{$id}";
}
}