pixxet/laravel-partialcache

Laravel 缓存部分 Blade 指令

v1.0.0 2020-01-08 17:23 UTC

This package is auto-updated.

Last update: 2024-09-09 05:41:45 UTC


README

Latest Version on Packagist Software License StyleCI Total Downloads

此包为 Laravel >=6.0 提供了一个 Blade 指令,用于缓存 Laravel 中的渲染部分。

安装

您可以通过 Composer 安装此包。

$ composer require pixxet/laravel-partialcache

在 Laravel 6.0 中,包的服务提供者和外观将自动注册。在旧版本的 Laravel 中,您必须手动注册它们。

// config/app.php

'providers' => [
  ...
  Pixxet\PartialCache\PartialCacheServiceProvider::class,
],

'aliases' => [
  ...
  'PartialCache' => Pixxet\PartialCache\PartialCacheFacade::class,
],

外观是可选的,但本指南的其他部分假定您正在使用它。

可选发布配置文件

$ php artisan vendor:publish --provider="Pixxet\PartialCache\PartialCacheServiceProvider"

用法

该包注册了一个 blade 指令,@cache。缓存指令接受与 @include 相同的参数,还包括可选参数,用于指定视图应缓存的时间(以分钟为单位)、渲染视图的唯一键和渲染视图的缓存标签。如果没有提供分钟数,视图将一直保留在缓存中,直到您手动从缓存中移除它。

注意,这缓存的是渲染后的 HTML,而不是像 blade 默认视图缓存那样的渲染后的 PHP。

{{-- Simple example --}}
@cache('footer.section.partial')

{{-- With extra view data --}}
@cache('products.card', ['product' => $category->products->first()])

{{-- With an added key (cache entry will be partialcache.user.profile.{$user->id}) --}}
@cache('user.profile', null, $user->id)

{{-- For a certain time --}}
{{-- (cache will invalidate in 60 minutes in this example, set null to remember forever) --}}
@cache('homepage.news', null, null, 60)

{{-- If template exists --}}
{{-- Just like the usual includeIf directive add "If" to the cache (or your custom directive) to cache partial only if the template exists --}}
@cacheIf('footer.section.partial')

{{-- When boolean true --}}
{{-- Just like the usual includeWhen directive add "When" to the cache (or your custom directive) to cache partial depending on a given boolean condition --}}
@cacheWhen($isPrintable !== true, 'footer.section.partial')

清除部分缓存

您可以使用 PartialCache::forget($view, $key) 忘记一个部分缓存条目。

PartialCache::forget('user.profile', $user->id);

如果您想清除所有条目,您需要清除您的整个缓存。

配置

配置不是必需的,但在配置文件中指定了三个选项

  • partialcache.enabled:完全启用或禁用缓存。默认为 true
  • partialcache.directive:要注册的 blade 指令的名称。默认为 cache
  • partialcache.key:用于缓存条目的基本键。默认为 partialcache
  • partialcache.default_duration:默认缓存持续时间(以分钟为单位),设置为 null 以永久保留。默认为 null

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件 me@omarsharkeyeh.me 而不是使用问题跟踪器来报告。

明信片软件

您可以自由使用此包。

鸣谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件