rodrigopedra/russian-doll-caching

v2.0.0 2018-02-16 00:46 UTC

This package is auto-updated.

Last update: 2024-09-13 02:53:54 UTC


README

高效视图缓存。

灵感来源于https://laracasts.com/series/russian-doll-caching-in-laravel的优秀系列。

观看该系列,了解其概念并正确使用它,例如:如何向上无效化缓存。

安装

在您的终端/shell中运行

composer require rodrigopedra/russian-doll-caching

如果您使用Laravel 5.3或5.4,您需要将服务提供者在您的config/app.php提供者数组中注册

// ... 

'providers' => [
    // ... 
    
    RodrigoPedra\RussianDollCaching\RussianDollCachingServiceProvider::class,
],

// ... 

配置

在您的终端/shell中运行

php artisan vendor:publish --provider="RodrigoPedra\RussianDollCaching\RussianDollCachingServiceProvider"

您可以配置一个should_cache约束,以便在本地开发时跳过缓存。

使用方法

在blade模板中使用@russian指令与使用@include指令相同。

您可以可选地提供一个自定义前缀,该前缀将被添加到缓存键之前。

    @russian('path.to.view', compact('user', 'articles'), 'version-prefix')

    @russian('path.to.other.view', compact('user', 'articles'))
    
    {{-- caution here, the cache key will only be the view name hash! --}}
    @russian('path.to.another.view')

您还可以传递一个array作为多个前缀,这些前缀将被添加到缓存项的键之前。

    @russian('path.to.other.view', compact('user'), [ 'v1', 'home' ])

如果您的缓存机制支持标记,如memcachedredis,所有缓存项都将使用russian标记进行缓存。

键计算

缓存机制将尝试使用data数组中的第一个元素作为缓存键的一部分。

您应该在您的模型中使用RussianCacheableModel特性或使用自定义集合中的RussianCacheableCollection。这些特性为这些对象添加了getCacheKey方法,因此您可以使用

常见问题解答

  • 为什么我的视图没有更新?

    尝试运行php artisan view:clearphp artisan cache:clear。同时,在开发时,将should_cache配置键设置为false

  • 我可以刷新仅由此包缓存的项吗?

    如果您使用支持标记的缓存机制(memcachedredis),所有缓存的项都将使用russian标记创建。因此,您可以通过在php artisan tinker中运行\Cache::tags('russian')->flush()来仅清除这些项。

许可

此软件包是开源软件,根据MIT许可授权。