Tor*_*enL 3 session bots redis laravel
我在Laravel大项目和Redis存储方面遇到问题。我们将会话存储在Redis中。我们已经有28GB的RAM。但是,由于我们搜索引擎机器人的点击量很高(每天超过25万次),因此它的运行速度仍然达到了极限。
有什么优雅的方法可以完全禁用机器人的会话?我已经实现了自己的会话中间件,如下所示:
<?php
namespace App\Http\Middleware;
use App\Custom\System\Visitor;
class StartSession extends \Illuminate\Session\Middleware\StartSession
{
protected function getSessionLifetimeInSeconds()
{
if(Visitor::isBot()) {
return 1;
}
return ($this->manager->getSessionConfig()['lifetime'] ?? null) * 60;
}
protected function sessionIsPersistent(array $config = null)
{
if(Visitor::isBot()) {
return false;
}
$config = $config ?: $this->manager->getSessionConfig();
return ! in_array($config['driver'], [null, 'array']);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我检测机器人的功能:
public static function isBot()
{
$exceptUserAgents = [
'Googlebot',
'Bingbot',
'Yahoo! Slurp',
'DuckDuckBot',
'Baiduspider',
'YandexBot',
'Sogou',
'facebot',
'ia_archiver',
];
if(!request()->header('User-Agent') || !str_contains(request()->header('User-Agent'), $exceptUserAgents)) {
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎不起作用。有人在这里有小费或经验吗?非常感谢你!
这就是我为自己解决的方法。
包括使用composer的自动程序检测程序包。我用了这个:https : //github.com/JayBizzle/Crawler-Detect
composer require jaybizzle/crawler-detect
创建一个新的中间件类
命名空间App \ Http \ Middleware;
类NoSessionForBotsMiddleware
{
公共功能句柄($ request,\ Closure $ next)
{
如果((新\ Jaybizzle \ CrawlerDetect \ CrawlerDetect)-> isCrawler()){
\ Config :: set('session.driver','array');
}
返回$ next($ request);
}
}
Kernel:受保护的$ middlewareGroups = [
'web'=> [
// ..
NoSessionForBotsMiddleware :: class,
StartSession :: class,
// ..
],
// ..
];
| 归档时间: |
|
| 查看次数: |
419 次 |
| 最近记录: |