优化过期Token删除

优化控制台DAU计算
pull/296/head
Karson 2021-03-28 16:04:50 +08:00
parent cd37a9e7e4
commit 120aa6b200
2 changed files with 17 additions and 11 deletions

View File

@ -47,8 +47,8 @@ class Dashboard extends Backend
'totalcategory' => \app\common\model\Category::count(), 'totalcategory' => \app\common\model\Category::count(),
'todayusersignup' => User::whereTime('jointime', 'today')->count(), 'todayusersignup' => User::whereTime('jointime', 'today')->count(),
'todayuserlogin' => User::whereTime('logintime', 'today')->count(), 'todayuserlogin' => User::whereTime('logintime', 'today')->count(),
'sevendau' => User::whereTime('jointime|logintime', '-7 days')->count(), 'sevendau' => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
'thirtydau' => User::whereTime('jointime|logintime', '-30 days')->count(), 'thirtydau' => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
'threednu' => User::whereTime('jointime', '-3 days')->count(), 'threednu' => User::whereTime('jointime', '-3 days')->count(),
'sevendnu' => User::whereTime('jointime', '-7 days')->count(), 'sevendnu' => User::whereTime('jointime', '-7 days')->count(),
'dbtablenums' => count($dbTableList), 'dbtablenums' => count($dbTableList),

View File

@ -36,13 +36,19 @@ class Mysql extends Driver
} else { } else {
$this->handler = \think\Db::name($this->options['table']); $this->handler = \think\Db::name($this->options['table']);
} }
$time = time();
$tokentime = cache('tokentime');
if (!$tokentime || $tokentime < $time - 86400) {
cache('tokentime', $time);
$this->handler->where('expiretime', '<', $time)->where('expiretime', '>', 0)->delete();
}
} }
/** /**
* 存储Token * 存储Token
* @param string $token Token * @param string $token Token
* @param int $user_id 会员ID * @param int $user_id 会员ID
* @param int $expire 过期时长,0表示无限,单位秒 * @param int $expire 过期时长,0表示无限,单位秒
* @return bool * @return bool
*/ */
public function set($token, $user_id, $expire = null) public function set($token, $user_id, $expire = null)
@ -50,12 +56,12 @@ class Mysql extends Driver
$expiretime = !is_null($expire) && $expire !== 0 ? time() + $expire : 0; $expiretime = !is_null($expire) && $expire !== 0 ? time() + $expire : 0;
$token = $this->getEncryptedToken($token); $token = $this->getEncryptedToken($token);
$this->handler->insert(['token' => $token, 'user_id' => $user_id, 'createtime' => time(), 'expiretime' => $expiretime]); $this->handler->insert(['token' => $token, 'user_id' => $user_id, 'createtime' => time(), 'expiretime' => $expiretime]);
return TRUE; return true;
} }
/** /**
* 获取Token内的信息 * 获取Token内的信息
* @param string $token * @param string $token
* @return array * @return array
*/ */
public function get($token) public function get($token)
@ -77,8 +83,8 @@ class Mysql extends Driver
/** /**
* 判断Token是否可用 * 判断Token是否可用
* @param string $token Token * @param string $token Token
* @param int $user_id 会员ID * @param int $user_id 会员ID
* @return boolean * @return boolean
*/ */
public function check($token, $user_id) public function check($token, $user_id)
@ -89,7 +95,7 @@ class Mysql extends Driver
/** /**
* 删除Token * 删除Token
* @param string $token * @param string $token
* @return boolean * @return boolean
*/ */
public function delete($token) public function delete($token)
@ -100,7 +106,7 @@ class Mysql extends Driver
/** /**
* 删除指定用户的所有Token * 删除指定用户的所有Token
* @param int $user_id * @param int $user_id
* @return boolean * @return boolean
*/ */
public function clear($user_id) public function clear($user_id)