1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 19:44:02 +08:00
course-tencent-cloud/app/Console/Tasks/CleanSessionTask.php
2020-09-22 16:29:09 +08:00

44 lines
879 B
PHP

<?php
namespace App\Console\Tasks;
class CleanSessionTask extends Task
{
public function mainAction()
{
$config = $this->getConfig();
$redis = $this->getRedis();
$redis->select($config->path('session.db'));
$keys = $this->querySessionKeys(10000);
if (count($keys) == 0) return;
$lifetime = $config->path('session.lifetime');
foreach ($keys as $key) {
$ttl = $redis->ttl($key);
$content = $redis->get($key);
if (empty($content) && $ttl < $lifetime * 0.5) {
$redis->del($key);
}
}
}
/**
* 查找待清理会话
*
* @param int $limit
* @return array
*/
protected function querySessionKeys($limit)
{
$cache = $this->getCache();
return $cache->queryKeys('_PHCR', $limit);
}
}