Skip to content

Commit c476281

Browse files
committed
Merge branch 'release/0.2.31'
2 parents 3b95d34 + 0309d51 commit c476281

12 files changed

+248
-136
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.31] - 18 Feb 2018
8+
### Added
9+
- optional cache key prefixing.
10+
711
## [0.2.30] - 18 Feb 2018
812
### Changed
913
- detection of Cachable trait to use `class_uses()` instead of looking for

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,37 @@ abstract class BaseModel
6868
}
6969
```
7070

71+
### Optional Cache Key Prefix
72+
Thanks to @lucian-dragomir for suggesting this feature! You can use cache key
73+
prefixing to keep cache entries separate for multi-tenant applications. For this
74+
it is recommended to add the Cachable trait to a base model, then set the cache
75+
key prefix config value there.
76+
77+
**Note that the config setting is included before the parent method is called,
78+
so that the setting is available in the parent as well.**
79+
80+
Here's is an example:
81+
```php
82+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
83+
84+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
85+
use Illuminate\Database\Eloquent\Model;
86+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
87+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
88+
89+
class BaseModel extends Model
90+
{
91+
use Cachable;
92+
93+
public function __construct($attributes = [])
94+
{
95+
config(['genealabs:laravel-model-caching' => 'test-prefix']);
96+
97+
parent::__construct($attributes);
98+
}
99+
}
100+
```
101+
71102
### Exception: User Model
72103
I would not recommend caching the user model, as it is a special case, since it
73104
extends `Illuminate\Foundation\Auth\User`. Overriding that would break functionality.

config/laravel-model-caching.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
return [
44
'store' => env('MODEL_CACHE_STORE'),
5+
6+
'cache-prefix' => '',
57
];

src/CacheKey.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@ class CacheKey
1010
protected $model;
1111
protected $query;
1212

13-
public function __construct(array $eagerLoad, Model $model, Builder $query)
13+
protected function getCachePrefix() : string
1414
{
15+
return "genealabs:laravel-model-caching:"
16+
. (config('genealabs:laravel-model-caching', '')
17+
? config('genealabs:laravel-model-caching', '') . ":"
18+
: "");
19+
}
20+
21+
public function __construct(
22+
array $eagerLoad,
23+
Model $model,
24+
Builder $query
25+
) {
1526
$this->eagerLoad = $eagerLoad;
1627
$this->model = $model;
1728
$this->query = $query;
@@ -22,7 +33,8 @@ public function make(
2233
$idColumn = null,
2334
string $keyDifferentiator = ''
2435
) : string {
25-
$key = $this->getModelSlug();
36+
$key = $this->getCachePrefix();
37+
$key .= $this->getModelSlug();
2638
$key .= $this->getIdColumn($idColumn ?: '');
2739
$key .= $this->getQueryColumns($columns);
2840
$key .= $this->getWhereClauses();

src/CacheTags.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ public function __construct(array $eagerLoad, Model $model)
1515
$this->model = $model;
1616
}
1717

18+
protected function getCachePrefix() : string
19+
{
20+
return "genealabs:laravel-model-caching:"
21+
. (config('genealabs:laravel-model-caching', '')
22+
? config('genealabs:laravel-model-caching', '') . ":"
23+
: "");
24+
}
25+
1826
public function make() : array
1927
{
20-
return collect($this->eagerLoad)
28+
$tags = collect($this->eagerLoad)
2129
->keys()
2230
->map(function ($relationName) {
2331
$relation = collect(explode('.', $relationName))
@@ -33,10 +41,16 @@ public function make() : array
3341
return $carry->{$name}();
3442
});
3543

36-
return str_slug(get_class($relation->getQuery()->getModel()));
44+
return $this->getCachePrefix()
45+
. str_slug(get_class($relation->getQuery()->getModel()));
3746
})
38-
->prepend(str_slug(get_class($this->model)))
47+
->prepend(
48+
$this->getCachePrefix()
49+
. str_slug(get_class($this->model))
50+
)
3951
->values()
4052
->toArray();
53+
54+
return $tags;
4155
}
4256
}

src/Traits/Cachable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
trait Cachable
1212
{
1313
protected $isCachable = true;
14-
protected static $isCachableKey = 'genealabs:model-caching:is-disabled';
14+
protected static $isCachableKey = 'genealabs:laravel-model-caching:is-disabled';
1515

1616
protected function cache(array $tags = [])
1717
{
@@ -89,7 +89,7 @@ public static function bootCachable()
8989

9090
public static function all($columns = ['*'])
9191
{
92-
if (cache()->get(self::$isCachableKey)) {
92+
if (! cache()->get(self::$isCachableKey)) {
9393
return parent::all($columns);
9494
}
9595

tests/Fixtures/BaseModel.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7+
8+
class BaseModel extends Model
9+
{
10+
use Cachable;
11+
12+
public function __construct($attributes = [])
13+
{
14+
config(['genealabs:laravel-model-caching' => 'test-prefix']);
15+
16+
parent::__construct($attributes);
17+
}
18+
}

tests/Fixtures/PrefixedAuthor.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Builder;
5+
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
use Illuminate\Database\Eloquent\Relations\HasOne;
7+
8+
class PrefixedAuthor extends BaseModel
9+
{
10+
use Cachable;
11+
12+
protected $fillable = [
13+
'name',
14+
'email',
15+
];
16+
protected $table = "authors";
17+
}

0 commit comments

Comments
 (0)