TemperWorks / laravel-russian-doll-cache by JeroenJochems

Laravel Russian Doll Cache Blade Directive
95,578
6
4
Package Data
Maintainer Username: JeroenJochems
Maintainer Contact: jeroen@temper.works (Jeroen Jochems)
Package Create Date: 2017-07-13
Package Last Update: 2019-10-16
Home Page: https://temper.works
Language: PHP
License: MIT
Last Refreshed: 2024-04-12 03:13:20
Package Statistics
Total Downloads: 95,578
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 6
Total Watchers: 4
Total Forks: 1
Total Open Issues: 0

Laravel Russian Doll Cache Blade Directive

Intro

This package provides a Blade directive to cache rendered partials in Laravel, based on the LRU auto evict policy of memcached (by default) or redis (optional)

In this implementation you don't need to explicitly set cache keys. The cache key will be automatically generated based on the data you pass to the partial.

If the data implements getCacheKey (by including the Cacheable trait) the key is based on the class, id and updated_at timestamp of the object. When the object changes, the key updates and the view will be regenerated on the next visit.

If the data does not implement getCacheKey, an md5 hash is used as cache key.

The cache store will be responsible for auto evicting the less recently used keys when the store is full.

Install

Make sure your cache store has max_memory configured and a proper eviction policy is set.

You can install the package via Composer:

$ composer require temperworks/laravel-russian-doll-cache

Start by registering the package's service provider and facade:

// config/app.php

'providers' => [
  ...
  TemperWorks\RussianDollCache\RussianDollCacheServiceProvider::class,
],

Usage

The package registers a blade directive, @cache. The cache directive accepts the same arguments as @include, plus optional parameters for the amount of minutes a view should be cached for. If no minutes are provided, the view will be remembered untill it gets deleted by the cache store.

Only the data you pass explicitly to the partial will be available within. Unlike the behavior of @include, global variables will be ignored to make sure all the variables will be represented in the cache key.

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

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

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

Clearing The PartialCache

Since we rely on the cache store to automatically flush old data, it's not needed to manually remove keys.

If you want to flush all entries, you'll need to either call PartialCache::flush() (note: this is only supported by drivers that support tags), or clear your entire cache.

Configuration

Configuration isn't necessary, but there are some options specified in the config file:

  • russian-doll-cache.enabled: Fully enable or disable the cache. Defaults to true.
  • russian-doll-cache.directive: The name of the blade directive to register. Defaults to cache.
  • russian-doll-cache.key: The base key that used for cache entries. Defaults to partialcache.

Credits

This package is forked from on spatie-partialcache by the awesome webdesign agency Spatie.

License

The MIT License (MIT). Please see License File for more information.