From d97bd7206e85dd4632ce0431ca9b8fd9cb39e775 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Thu, 11 Feb 2016 12:50:56 -0200 Subject: [PATCH] added shlud_cache configuration option --- readme.md | 24 +++++++++++++++---- src/RussianDollCaching/RussianDollCaching.php | 15 +++++++++--- .../RussianDollCachingServiceProvider.php | 6 ++++- src/RussianDollCaching/config.php | 12 ++++++++++ 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/RussianDollCaching/config.php diff --git a/readme.md b/readme.md index b3192fe..fb6e18b 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,7 @@ # Russian Doll Caching for Laravel 5.1 LTS and Laravel 5.2 +Efficient view caching. + Inspired by the excellent series at https://laracasts.com/series/russian-doll-caching-in-laravel . Watch that series to understand the concepts around it and to use it properly, @@ -7,24 +9,36 @@ for example: how to invalidate the cache upwards. ## Installation +In your terminal/shell run: + ``` composer require rodrigopedra/russian-doll-caching ``` -## Configuration - -Add the provider to your config/app.php: +Add the provider to your config/app.php service providers array: ```php -// in your config/app.php add the provider to the service providers key +/* ... */ 'providers' => [ /* ... */ RodrigoPedra\RussianDollCaching\RussianDollCachingServiceProvider::class, -] +], + +/* ... */ +``` + +## Configuration + +In your terminal/shell run: + +``` +php artisan vendor:publish --provider="RodrigoPedra\RussianDollCaching\RussianDollCachingServiceProvider" ``` +You can configure a `should_cache` constraint, so you can skip caching, for example, while developing locally. + ## Usage Use the `@russian` directive in your blade templates the same way you would use the `@include` directive. diff --git a/src/RussianDollCaching/RussianDollCaching.php b/src/RussianDollCaching/RussianDollCaching.php index 5ba16e7..190f1b7 100644 --- a/src/RussianDollCaching/RussianDollCaching.php +++ b/src/RussianDollCaching/RussianDollCaching.php @@ -16,16 +16,21 @@ class RussianDollCaching /** @var View */ protected $view; + /** @var bool */ + protected $shouldCache; + /** * RussianDollCaching constructor. * * @param \Illuminate\Contracts\Cache\Repository $cache * @param \Illuminate\Contracts\View\Factory $view + * @param bool $shouldCache */ - public function __construct( Cache $cache, View $view ) + public function __construct( Cache $cache, View $view, $shouldCache = true ) { - $this->cache = $cache; - $this->view = $view; + $this->cache = $cache; + $this->view = $view; + $this->shouldCache = $shouldCache; } /** @@ -41,6 +46,10 @@ public function get( $view, $data, $prefix = null ) { $key = $this->makeKey( $view, (array)$data, $prefix ); + if (!$this->shouldCache) { + return $this->view->make( $view, $data )->render(); + } + return $this->getCache()->rememberForever( $key, function () use ( $view, $data ) { return $this->view->make( $view, $data )->render(); } ); diff --git a/src/RussianDollCaching/RussianDollCachingServiceProvider.php b/src/RussianDollCaching/RussianDollCachingServiceProvider.php index f748aaa..a5d2398 100644 --- a/src/RussianDollCaching/RussianDollCachingServiceProvider.php +++ b/src/RussianDollCaching/RussianDollCachingServiceProvider.php @@ -8,6 +8,8 @@ class RussianDollCachingServiceProvider extends ServiceProvider { public function boot() { + $this->publishes( [ __DIR__ . DIRECTORY_SEPARATOR . 'config.php' => config_path( 'russian.php' ) ] ); + $this->app[ 'blade.compiler' ]->directive( 'russian', function ( $expression ) { $className = RussianDollCaching::class; @@ -17,8 +19,10 @@ public function boot() public function register() { + $this->mergeConfigFrom( __DIR__ . DIRECTORY_SEPARATOR . 'config.php', 'russian' ); + $this->app->singleton( RussianDollCaching::class, function ( $app ) { - return new RussianDollCaching( $app[ 'cache.store' ], $app[ 'view' ] ); + return new RussianDollCaching( $app[ 'cache.store' ], $app[ 'view' ], !!config( 'russian.should_cache' ) ); } ); } } diff --git a/src/RussianDollCaching/config.php b/src/RussianDollCaching/config.php new file mode 100644 index 0000000..d1b42cb --- /dev/null +++ b/src/RussianDollCaching/config.php @@ -0,0 +1,12 @@ + app('env') !== 'local', + */ + + 'should_cache' => true, +];