1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-07 17:31:12 +08:00
jacky huang a93ce8e293
v1.2.3 (#19)
* 去除无用的auth_url

* 修改readme

* 修复计划任务生成sitemap.xml失败问题

* 增加课程推荐

* 增加后台刷新首页缓存小工具

* 调整公众号模板消息

* 增加刷新首页推荐课程缓存逻辑

* 更新课程综合评分算法

* 修复在线用户并发重复记录问题

* 增加限制共享帐号功能

* 修复数据迁移中无符号整型问题

* 更新版本为v1.2.3
2021-01-03 15:13:40 +08:00

79 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Library\Utils;
use App\Library\Cache\Backend\Redis as RedisCache;
use Phalcon\Di;
use Phalcon\Text;
class Lock
{
/**
* @param string $itemId
* @param int $expire
* @return bool|string
*/
public static function addLock($itemId, $expire = 600)
{
if (!$itemId || $expire <= 0) {
return false;
}
/**
* @var RedisCache $cache
*/
$cache = Di::getDefault()->getShared('cache');
$redis = $cache->getRedis();
$lockId = Text::random(Text::RANDOM_ALNUM, 16);
$keyName = self::getLockKey($itemId);
$result = $redis->set($keyName, $lockId, ['nx', 'ex' => $expire]);
return $result ? $lockId : false;
}
/**
* @param string $itemId
* @param string $lockId
* @return bool
*/
public static function releaseLock($itemId, $lockId)
{
if (!$itemId || !$lockId) {
return false;
}
/**
* @var RedisCache $cache
*/
$cache = Di::getDefault()->getShared('cache');
$redis = $cache->getRedis();
$keyName = self::getLockKey($itemId);
$redis->watch($keyName);
/**
* 监听key防止被修改或删除提交事务后会自动取消监控其他情况需手动解除监控
*/
if ($lockId == $redis->get($keyName)) {
$redis->multi()->del($keyName)->exec();
return true;
}
$redis->unwatch();
return false;
}
public static function getLockKey($itemId)
{
return sprintf('kg_lock:%s', $itemId);
}
}