项目中有计划任务的话, 可以添加redis的分布式锁setnx和 expire去限制, 防止多服务器中任务冲突/重复跑等情况
Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。
返回值
设置成功,返回 1 。 设置失败,返回 0 。
Redis Expire 命令用于设置 key 的过期时间,key 过期后将不再可用。单位以秒计。
返回值
设置成功返回 1 。 当 key 不存在或者不能为 key 设置过期时间时(比如在低于 2.1.3 版本的 Redis 中你尝试更新 key 的过期时间)返回 0 。
public function handle()
{
Log::info($this->description . ' start ');
$t = time();
$t = date('Hi', $t);
$manage = GymShopsRestMan::where(['id' => 1])->first();
$redis = Redis::connection();
$rest_no_running_key = $this->signature . ':running';
if ($redis->setnx($rest_no_running_key, 123) === 1) {
$expire_res = $redis->expire($rest_no_running_key, env('GYM_CREATE_ORDER_NO_RUNNING_TIMES', 1200));
if ($expire_res !== 1) goto expire_fail;
if ($manage->rest_end_time < $t && $manage->shop_status != 0) {
GymShopsRestManage::where(['id' => 1])->update(['shop_status' => 0]);
}
expire_fail:
$redis->del($rest_no_running_key);
}
Log::info($this->description . ' stop ');
}
756

被折叠的 条评论
为什么被折叠?



