Skip to content

Commit

Permalink
update to laravel 5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed Feb 16, 2018
1 parent bbf1a2f commit 4bf5a1f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 69 deletions.
50 changes: 29 additions & 21 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
{
"name": "rodrigopedra/russian-doll-caching",
"description": "Laravel Russian Doll Caching",
"keywords": ["russian doll caching", "rodrigopedra", "laravel"],
"license": "MIT",
"authors": [
{
"name": "Rodrigo Pedra Brum",
"email": "[email protected]"
"name": "rodrigopedra/russian-doll-caching",
"description": "Laravel Russian Doll Caching",
"keywords": [
"russian doll caching",
"rodrigopedra",
"laravel"
],
"license": "MIT",
"authors": [
{
"name": "Rodrigo Pedra Brum",
"email": "[email protected]"
}
],
"require": {
"illuminate/contracts": "~5.3|~5.4|~5.5|~5.6",
"illuminate/support": "~5.3|~5.4|~5.5|~5.6"
},
"autoload": {
"psr-4": {
"RodrigoPedra\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"RodrigoPedra\\RussianDollCaching\\RussianDollCachingServiceProvider"
]
}
}
],
"require": {
"illuminate/support": "~5.1|~5.2",
"illuminate/database": "~5.1|~5.2",
"illuminate/cache": "~5.1|~5.2",
"nesbot/carbon": "~1.19"
},
"autoload": {
"psr-4": {
"RodrigoPedra\\": "src/"
}
},
"minimum-stability": "dev"
}
31 changes: 14 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Russian Doll Caching for Laravel 5.1 LTS and Laravel 5.2
# Russian Doll Caching for Laravel 5.3, 5.4, 5.5 and 5.6

Efficient view caching.

Expand All @@ -15,18 +15,19 @@ In your terminal/shell run:
composer require rodrigopedra/russian-doll-caching
```

Add the provider to your config/app.php service providers array:
If you're using Laravel 5.3 or 5.4 you need to register the service provider in
your `config/app.php` providers array:

```php
/* ... */
// ...

'providers' => [
/* ... */
// ...

RodrigoPedra\RussianDollCaching\RussianDollCachingServiceProvider::class,
],

/* ... */
// ...
```

## Configuration
Expand Down Expand Up @@ -54,29 +55,25 @@ You can optionally inform a custom prefix that will be prepended to the cache ke
@russian('path.to.another.view')
```

***IMPORTANT*** if no prefix is informed and the first element in the `data` array is not an Eloquent model,
then the view will be cached only by its name hash which can lead to unexpected behavior.

You can also pass multiple prefixes as an `array`, that will be prepended to the cache item's key:

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

If your cache mechanism supports tagging, like `memcached` or `redis`, all cache items will be cached with a `russian` tag.
If your cache mechanism supports tagging, like `memcached` or `redis`, all cache items will be
cached with a `russian` tag.

You can inform an `array` of additional tags as the fourth parameter:

```php
@russian('path.to.other.view', compact('user'), 'v3', ['tenant-1'])
```
### Key calculation

## FAQ
The caching mechanism will try to use the first element in the `data` array as part of the cache key.

You should use the `RussianCacheableModel` trait in your models or use the `RussianCacheableCollection` in
your custom collections. This traits add a `getCacheKey` method to this objects so you can

- Why name this directive `@russian` and not `@cache`?

As a directive named `@cache` can be added to the official Laravel in a future release, this choice aims
to avoid any conflicts.
## FAQ

- Why are my views not updating?

Expand Down
51 changes: 23 additions & 28 deletions src/RussianDollCaching/RussianDollCaching.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace RodrigoPedra\RussianDollCaching;

use Carbon\Carbon;
use Illuminate\Cache\TaggableStore;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\View\Factory as View;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\View\Factory as View;

class RussianDollCaching
{
Expand Down Expand Up @@ -38,21 +35,24 @@ public function __construct( Cache $cache, View $view, $shouldCache = true )
*
* @param string $view
* @param mixed|null $data
* @param mixed|null $prefix
* @param mixed|null $tags
* @param mixed|null $prefixes
*
* @return string
*/
public function get( $view, $data = null, $prefix = null, $tags = null )
public function get( $view, $data = null, $prefixes = null )
{
$key = $this->makeKey( $view, (array)$data, (array)$prefix );
$data = array_wrap( $data );

if (!$this->shouldCache) {
return $this->view->make( $view, $data )->render();
}

return $this->getCache( (array)$tags )->rememberForever( $key, function () use ( $view, $data ) {
return $this->view->make( $view, (array)$data )->render();
$prefixes = array_wrap( $prefixes );

$key = $this->normalizeCacheKey( $view, $data, $prefixes );

return $this->getCache()->rememberForever( $key, function () use ( $view, $data ) {
return $this->view->make( $view, $data )->render();
} );
}

Expand All @@ -61,39 +61,34 @@ public function get( $view, $data = null, $prefix = null, $tags = null )
*
* @param string $view
* @param array $data
* @param array $prefix
* @param array $prefixes
*
* @return string
*/
protected function makeKey( $view, array $data, array $prefix )
protected function normalizeCacheKey( $view, array $data, array $prefixes )
{
$parts = array_merge( $prefix, [ md5( $view ) ] );
$item = reset( $data );

$model = reset( $data );

if ($model instanceof Model) {
// will generate a new cache until $model->updated_at is not null
$timestamp = $model->updated_at ?: Carbon::now();

// use the + array union operator
// @see http://php.net/manual/en/function.array-merge.php
$parts = array_merge( $parts, [ get_class( $model ), $model->getKey(), $timestamp->timestamp ] );
if (is_object( $item ) && method_exists( $item, 'getCacheKey' )) {
array_push( $prefixes, $item->getCacheKey() );
} elseif (is_object( $item ) && method_exists( $item, '__toString' )) {
array_push( $prefixes, md5( $item ) );
}

return join( '/', $parts );
array_push( $prefixes, md5( $view ) );

return join( '/', $prefixes );
}

/**
* Returns the current cache instance
*
* @param array $tags
*
* @return Cache
*/
protected function getCache( array $tags )
protected function getCache()
{
if ($this->cache instanceof TaggableStore) {
return $this->cache->tags( array_merge( [ 'russian' ], $tags ) );
if (method_exists( $this->cache, 'tags' )) {
return $this->cache->tags( 'russian' );
}

return $this->cache;
Expand Down
6 changes: 3 additions & 3 deletions src/RussianDollCaching/RussianDollCachingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public function boot()
{
$this->publishes( [ __DIR__ . DIRECTORY_SEPARATOR . 'config.php' => config_path( 'russian.php' ) ] );

Blade::directive( 'russian', function ( $expression ) {
$className = RussianDollCaching::class;
$className = RussianDollCaching::class;

return "<?php echo app('{$className}')->get{$expression}; ?>";
Blade::directive( 'russian', function ( $expression ) use ( $className ) {
return "<?php echo app('{$className}')->get({$expression}); ?>";
} );
}

Expand Down
17 changes: 17 additions & 0 deletions src/RussianDollCaching/Traits/RussianCacheableCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace RodrigoPedra\RussianDollCaching\Traits;

trait RussianCacheableCollection
{
/**
* Calculate a unique cache key for the model instance.
*/
public function getCacheKey()
{
return sprintf( "%s/%s",
get_class( $this ),
md5( $this->toJson() )
);
}
}
18 changes: 18 additions & 0 deletions src/RussianDollCaching/Traits/RussianCacheableModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace RodrigoPedra\RussianDollCaching\Traits;

trait RussianCacheableModel
{
/**
* Calculate a unique cache key for the model instance.
*/
public function getCacheKey()
{
return sprintf( "%s/%s-%s",
get_class( $this ),
$this->getKey(),
$this->updated_at->timestamp
);
}
}

0 comments on commit 4bf5a1f

Please sign in to comment.