ryssbowh/php-cache-warmer

异步 PHP 缓存预热器

1.0.1 2021-01-24 08:55 UTC

This package is auto-updated.

Last update: 2024-09-24 16:44:10 UTC


README

简单的 PHP 缓存预热器,添加 URL 或解析站点地图并异步访问所有 URL。您需要等待 warm 方法返回的承诺完成

use Ryssbowh\PhpCacheWarmer\Warmer;

$warmer = new Warmer();
$warmer->parseSitemap('http://mysite.com/sitemap.xml')
	->addUrls('http://othersite.com')
	->addUrls([
		'http://othersite.com/blog',
		'http://othersite.com/hello'
	])
	->ignoreUrls('http://mysite.com/page-503')
	->ignoreUrls([
		'http://mysite.com/page-400',
		'http://mysite.com/page-500'
	])
	->ignoreRegexs('/http:\/\/mysite\.com\/page*/')
	->ignoreRegexs([
		'/http:\/\/mysite\.com\/forum*/',
		'/http:\/\/mysite\.com\/blog*/'
	]);
$warmer->warm()->wait();

您可以定义并发请求数量(默认 25)

$warmer = new Warmer(50);

您可以在构造函数中传递 Guzzle 选项

$warmer = new Warmer(25, ['headers' => [
	'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
]]);

并订阅一个观察者,它将在每次成功和失败的请求时被调用

use Ryssbowh\PhpCacheWarmer\Warmer;
use Ryssbowh\PhpCacheWarmer\Observer;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Response;

class MyObserver implements Observer
{
	public function onFulfilled(Response $response, string $url)
	{
		echo 'visited '.$url;
	}

	public function onRejected(RequestException $reason, string $url)
	{
		echo 'failed '.$url.' with code '.$reason->getResponse()->getStatusCode();
	}
}

$warmer = new Warmer(25, [], new MyObserver);

感谢伟大的 vipnytt/sitemapparser 站点地图解析器 :)