Giter VIP home page Giter VIP logo

laravel-partialcache's Introduction

Laravel Cache Partial Blade Directive

Latest Version on Packagist Software License StyleCI Total Downloads

This package provides a Blade directive for Laravel >=6.0 to cache rendered partials in Laravel.

Install

You can install the package via Composer:

$ composer require pixxet/laravel-partialcache

In Laravel 6.0 the package's service provider and facade will be registered automatically. In older versions of Laravel, you must register them manually:

// config/app.php

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

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

The facade is optional, but the rest of this guide assumes you're using it.

Optionally publish the config files:

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

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, a key unique to the rendered view, and a cache tag for the rendered view. If no minutes are provided, the view will be remembered until you manually remove it from the cache.

Note that this caches the rendered html, not the rendered php like blade's default view caching.

{{-- 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')

Clearing The PartialCache

You can forget a partial cache entry with PartialCache::forget($view, $key).

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

If you want to flush all entries, you'll need to clear your entire cache.

Configuration

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

  • partialcache.enabled: Fully enable or disable the cache. Defaults to true.
  • partialcache.directive: The name of the blade directive to register. Defaults to cache.
  • partialcache.key: The base key that used for cache entries. Defaults to partialcache.
  • partialcache.default_duration: The default cache duration in minutes, set null to remember forever. Defaults to null.

Change log

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package.

Credits

License

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

laravel-partialcache's People

Contributors

pixxet avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.