perturbatio/cachetags

为Laravel的Blade模板语言或普通PHP使用辅助工具提供部分缓存机制

v2.1.0 2022-07-29 16:34 UTC

This package is auto-updated.

Last update: 2024-08-29 02:31:44 UTC


README

CircleCI Latest Stable Version Latest Unstable Version License Total Downloads

此包旨在允许您通过使用Blade指令标记来缓存网页的部分内容。

您还可以指定部分缓存的时间以及命名它(以便在需要时使其失效)。

嵌套cacheTags也是可能的,这意味着内部内容将至少与外部缓存一样长

安装

使用Composer包安装

composer require perturbatio/cachetags

Perturbatio\CacheTags\CacheTagsProvider::class添加到您的config/app.php提供者数组中

'CacheTags' => Perturbatio\CacheTags\CacheTags::class添加到您的别名中

然后使用php artisan vendor:publish --tag=config发布配置文件,这将在/config目录中创建一个cachetag.php配置文件

使用方法

缓存项

Blade

@cachetagStart('super-cool-widget', 15) <!-- widget cached for 15 minutes -->
<?=superCoolWidgetThatTakesTooLongToGenerate();?>

	@cachetagStart('other-cool-widget', 'forever') <!-- widget cached until something clears it, nested inside the outer cache -->
	<?=otherCoolWidgetThatTakesTooLongToGenerate();?>
	@cachetagEnd()
	
@cachetagEnd()

PHP

if ( cachetagHas('super-cool-widget') ){
	echo cachetagGet('super-cool-widget');
} else {
	cachetagStart('super-cool-widget', 15);
	echo superCoolWidgetThatTakesTooLongToGenerate();
  
		if ( cachetagHas('other-cool-widget') ){
			echo cachetagGet('other-cool-widget');
		} else {
			cachetagStart('other-cool-widget', 'forever'); //widget cached until something clears it, nested inside the outer cache 
			echo otherCoolWidgetThatTakesTooLongToGenerate();
			echo cachetagEnd();
		}
		
	echo cachetagEnd();
}

清除项

Blade

@cachetagClear('super-cool-widget')

PHP

//clear the cache for a specific key
cachetagClear('super-cool-widget');

if ( $otherCoolWidgetNeedsCacheInvalidated ){ //conditionally clear the 
	cachetagClear('other-cool-widget');
}