Skip to content

Commit c5b3240

Browse files
committed
Refactor out all caching functionality to trait
1 parent 5590d6b commit c5b3240

File tree

3 files changed

+37
-45
lines changed

3 files changed

+37
-45
lines changed

src/CachedBuilder.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class CachedBuilder extends EloquentBuilder
1010
{
1111
use Cachable;
1212

13-
protected $isCachable = true;
14-
1513
public function avg($column)
1614
{
1715
if (! $this->isCachable) {
@@ -56,13 +54,6 @@ public function delete()
5654
return parent::delete();
5755
}
5856

59-
public function disableCache()
60-
{
61-
$this->isCachable = false;
62-
63-
return $this;
64-
}
65-
6657
/**
6758
* @SuppressWarnings(PHPMD.ShortVariable)
6859
*/
@@ -155,16 +146,4 @@ public function sum($column)
155146
return parent::sum($column);
156147
});
157148
}
158-
159-
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
160-
{
161-
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
162-
->make($columns, $idColumn);
163-
}
164-
165-
protected function makeCacheTags() : array
166-
{
167-
return (new CacheTags($this->eagerLoad, $this->model))
168-
->make();
169-
}
170149
}

src/CachedModel.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

33
use GeneaLabs\LaravelModelCaching\CachedBuilder as Builder;
4+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
45
use Illuminate\Cache\CacheManager;
56
use Illuminate\Cache\TaggableStore;
67
use Illuminate\Cache\TaggedCache;
@@ -12,6 +13,8 @@
1213

1314
abstract class CachedModel extends Model
1415
{
16+
use Cachable;
17+
1518
public function newEloquentBuilder($query)
1619
{
1720
if (session('genealabs-laravel-model-caching-is-disabled')) {
@@ -46,30 +49,6 @@ public static function boot()
4649
});
4750
}
4851

49-
public function cache(array $tags = [])
50-
{
51-
$cache = cache();
52-
53-
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
54-
array_push($tags, str_slug(get_called_class()));
55-
$cache = $cache->tags($tags);
56-
}
57-
58-
return $cache;
59-
}
60-
61-
public function disableCache() : self
62-
{
63-
session(['genealabs-laravel-model-caching-is-disabled' => true]);
64-
65-
return $this;
66-
}
67-
68-
public function flushCache(array $tags = [])
69-
{
70-
$this->cache($tags)->flush();
71-
}
72-
7352
public static function all($columns = ['*'])
7453
{
7554
$class = get_called_class();

src/Traits/Cachable.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
22

3+
use GeneaLabs\LaravelModelCaching\CacheKey;
4+
use GeneaLabs\LaravelModelCaching\CacheTags;
5+
use GeneaLabs\LaravelModelCaching\CachedModel;
36
use Illuminate\Cache\TaggableStore;
47

58
trait Cachable
69
{
10+
protected $isCachable = true;
11+
712
protected function cache(array $tags = [])
813
{
914
$cache = cache();
1015

1116
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
17+
if (is_a($this, CachedModel::class)) {
18+
array_push($tags, str_slug(get_called_class()));
19+
}
20+
1221
$cache = $cache->tags($tags);
1322
}
1423

1524
return $cache;
1625
}
26+
27+
public function disableCache()
28+
{
29+
session(['genealabs-laravel-model-caching-is-disabled' => true]);
30+
$this->isCachable = false;
31+
32+
return $this;
33+
}
34+
35+
public function flushCache(array $tags = [])
36+
{
37+
$this->cache($tags)->flush();
38+
}
39+
40+
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
41+
{
42+
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
43+
->make($columns, $idColumn);
44+
}
45+
46+
protected function makeCacheTags() : array
47+
{
48+
return (new CacheTags($this->eagerLoad, $this->model))
49+
->make();
50+
}
1751
}

0 commit comments

Comments
 (0)