1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Neurony\QueryCache\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder; |
6
|
|
|
use Neurony\QueryCache\Database\QueryCacheBuilder; |
7
|
|
|
|
8
|
|
|
trait IsCacheable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Boot the model. |
12
|
|
|
* |
13
|
|
|
* @return void |
14
|
|
|
*/ |
15
|
|
|
public static function bootIsCacheable() |
16
|
|
|
{ |
17
|
|
|
static::saved(function ($model) { |
18
|
|
|
$model->clearQueryCache(); |
19
|
|
|
}); |
20
|
|
|
|
21
|
|
|
static::deleted(function ($model) { |
22
|
|
|
$model->clearQueryCache(); |
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function getQueryCacheTag(): string |
30
|
|
|
{ |
31
|
|
|
return app('cache.query')->getAllQueryCachePrefix().'.'.(string) $this->getTable(); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getDuplicateQueryCacheTag(): string |
38
|
|
|
{ |
39
|
|
|
return app('cache.query')->getDuplicateQueryCachePrefix().'.'.(string) $this->getTable(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Flush the query cache from Redis only for the tag corresponding to the model instance. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function clearQueryCache(): void |
48
|
|
|
{ |
49
|
|
|
app('cache.query')->clearQueryCache($this); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a new query builder instance for the connection. |
54
|
|
|
* |
55
|
|
|
* @return Builder |
56
|
|
|
*/ |
57
|
|
|
protected function newBaseQueryBuilder(): Builder |
58
|
|
|
{ |
59
|
|
|
$cacheAllQueriesForever = false; |
60
|
|
|
$cacheOnlyDuplicateQueriesOnce = false; |
61
|
|
|
|
62
|
|
|
$connection = $this->getConnection(); |
|
|
|
|
63
|
|
|
$grammar = $connection->getQueryGrammar(); |
64
|
|
|
|
65
|
|
|
if (app('cache.query')->canCacheQueries()) { |
66
|
|
|
if (app('cache.query')->shouldCacheAllQueries()) { |
67
|
|
|
$cacheAllQueriesForever = true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (app('cache.query')->shouldCacheDuplicateQueries()) { |
71
|
|
|
$cacheOnlyDuplicateQueriesOnce = true; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($cacheAllQueriesForever === true) { |
76
|
|
|
return new QueryCacheBuilder( |
77
|
|
|
$connection, $grammar, $connection->getPostProcessor(), |
78
|
|
|
$this->getQueryCacheTag(), app('cache.query')->cacheAllQueriesForeverType() |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($cacheOnlyDuplicateQueriesOnce === true) { |
83
|
|
|
return new QueryCacheBuilder( |
84
|
|
|
$connection, $grammar, $connection->getPostProcessor(), |
85
|
|
|
$this->getDuplicateQueryCacheTag(), app('cache.query')->cacheOnlyDuplicateQueriesOnceType() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return parent::newBaseQueryBuilder(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.