1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-29 22:01:38 +08:00

1.优化CsrfToken

2.去除无实际作用的OptimizeTable脚本
This commit is contained in:
koogua 2022-07-22 16:03:43 +08:00
parent f5719c2ee7
commit dc85029241
4 changed files with 20 additions and 144 deletions

View File

@ -1,139 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Console\Tasks;
use App\Models\ImMessage as ImMessageModel;
use App\Models\Learning as LearningModel;
use App\Models\Task as TaskModel;
use App\Models\UserSession as UserSessionModel;
use App\Models\UserToken as UserTokenModel;
class OptimizeTableTask extends Task
{
public function mainAction()
{
$this->optimizeUserSessionTable();
$this->optimizeUserTokenTable();
$this->optimizeImMessageTable();
$this->optimizeLearningTable();
$this->optimizeTaskTable();
}
protected function optimizeUserSessionTable()
{
$sessionModel = new UserSessionModel();
$tableName = $sessionModel->getSource();
if (UserSessionModel::count() < 1000000) {
echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL;
return;
}
echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL;
$this->db->delete($tableName, 'expire_time < :expire_time', [
'expire_time' => strtotime('-3 days'),
]);
$this->db->execute("OPTIMIZE TABLE {$tableName}");
echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL;
}
protected function optimizeUserTokenTable()
{
$tokenModel = new UserTokenModel();
$tableName = $tokenModel->getSource();
if (UserTokenModel::count() < 1000000) {
echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL;
return;
}
echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL;
$this->db->delete($tableName, 'expire_time < :expire_time', [
'expire_time' => strtotime('-3 days'),
]);
$this->db->execute("OPTIMIZE TABLE {$tableName}");
echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL;
}
protected function optimizeImMessageTable()
{
$messageModel = new ImMessageModel();
$tableName = $messageModel->getSource();
if (ImMessageModel::count() < 1000000) {
echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL;
return;
}
echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL;
$this->db->delete($tableName, 'create_time < :create_time', [
'create_time' => strtotime('-6 months'),
]);
$this->db->execute("OPTIMIZE TABLE {$tableName}");
echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL;
}
protected function optimizeLearningTable()
{
$learningModel = new LearningModel();
$tableName = $learningModel->getSource();
if (LearningModel::count() < 1000000) {
echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL;
return;
}
echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL;
$this->db->delete($tableName, 'create_time < :create_time', [
'create_time' => strtotime('-6 months'),
]);
$this->db->execute("OPTIMIZE TABLE {$tableName}");
echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL;
}
protected function optimizeTaskTable()
{
$taskModel = new TaskModel();
$tableName = $taskModel->getSource();
if (TaskModel::count() < 1000000) {
echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL;
return;
}
echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL;
$this->db->delete($tableName, 'create_time < :create_time AND status > :status', [
'create_time' => strtotime('-6 months'),
'status' => TaskModel::STATUS_PENDING,
]);
$this->db->execute("OPTIMIZE TABLE {$tableName}");
echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL;
}
}

View File

@ -7,6 +7,7 @@
namespace App\Library; namespace App\Library;
use Phalcon\Config;
use Phalcon\Crypt; use Phalcon\Crypt;
use Phalcon\Di; use Phalcon\Di;
use Phalcon\Text; use Phalcon\Text;
@ -19,7 +20,7 @@ class CsrfToken
*/ */
protected $crypt; protected $crypt;
protected $lifetime = 600; protected $lifetime = 86400;
protected $delimiter = '@@'; protected $delimiter = '@@';
@ -33,7 +34,7 @@ class CsrfToken
public function getToken() public function getToken()
{ {
$content = [ $content = [
time() + $this->lifetime, $this->getExpiredTime(),
$this->fixed, $this->fixed,
Text::random(8), Text::random(8),
]; ];
@ -62,4 +63,16 @@ class CsrfToken
return true; return true;
} }
protected function getExpiredTime()
{
/**
* @var $config Config
*/
$config = Di::getDefault()->getShared('config');
$lifetime = $config->path('csrf_token.lifetime') ?: $this->lifetime;
return $lifetime + time();
}
} }

View File

@ -117,6 +117,11 @@ $config['metadata']['lifetime'] = 7 * 86400;
*/ */
$config['annotation']['lifetime'] = 7 * 86400; $config['annotation']['lifetime'] = 7 * 86400;
/**
* CsrfToken有效期
*/
$config['csrf_token']['lifetime'] = 86400;
/** /**
* 允许跨域 * 允许跨域
*/ */

View File

@ -99,7 +99,4 @@ $scheduler->php($script, $bin, ['--task' => 'sitemap', '--action' => 'main'])
$scheduler->php($script, $bin, ['--task' => 'teacher_live_notice', '--action' => 'provide']) $scheduler->php($script, $bin, ['--task' => 'teacher_live_notice', '--action' => 'provide'])
->daily(4, 7); ->daily(4, 7);
$scheduler->php($script, $bin, ['--task' => 'optimize_table', '--action' => 'main'])
->weekly(6, 5, 3);
$scheduler->run(); $scheduler->run();