Skip to content

Commit

Permalink
added shlud_cache configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed Feb 11, 2016
1 parent d823e63 commit d97bd72
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
24 changes: 19 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
# 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,
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.
Expand Down
15 changes: 12 additions & 3 deletions src/RussianDollCaching/RussianDollCaching.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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();
} );
Expand Down
6 changes: 5 additions & 1 deletion src/RussianDollCaching/RussianDollCachingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' ) );
} );
}
}
12 changes: 12 additions & 0 deletions src/RussianDollCaching/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
/**
* Use this to skip caching under some circunstances. For example you can avoid caching
* the views in the local environment while developing changing the condition to:
*
* 'should_cache' => app('env') !== 'local',
*/

'should_cache' => true,
];

0 comments on commit d97bd72

Please sign in to comment.