mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-06 01:52:48 +08:00
优化定时器的防刷机制;
This commit is contained in:
35
app/common/controller/TimerController.php
Normal file
35
app/common/controller/TimerController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\controller;
|
||||
|
||||
use think\facade\Cache;
|
||||
|
||||
class TimerController extends ToolsController
|
||||
{
|
||||
|
||||
protected $frequency = null;
|
||||
|
||||
public function initialize()
|
||||
{
|
||||
if (is_integer($this->frequency)) {
|
||||
$this->protectVisit($this->frequency);
|
||||
}
|
||||
}
|
||||
|
||||
protected function protectVisit(int $frequency)
|
||||
{
|
||||
|
||||
$cache_tag = 'timer_protect';
|
||||
|
||||
$cache_key = 'timer_protect_' . $this->request->url();
|
||||
|
||||
$last_exec_time = Cache::get($cache_key, 0);
|
||||
|
||||
if ($last_exec_time >= time() - $frequency) {
|
||||
|
||||
return $this->error('请不要频繁请求');
|
||||
}
|
||||
|
||||
Cache::tag($cache_tag)->set($cache_key, time());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user