1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Suitmedia\Cacheable\Traits\Repository; |
4
|
|
|
|
5
|
|
|
trait CacheableTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Get the base class name, without 'Repository' suffix. |
9
|
|
|
* |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
|
|
protected function baseClassName() |
13
|
|
|
{ |
14
|
|
|
$class = class_basename(get_class($this)); |
15
|
|
|
|
16
|
|
|
if (ends_with($class, 'Repository')) { |
17
|
|
|
$class = substr($class, 0, -10); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return $class; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return the cache duration value |
25
|
|
|
* which would be used by the repository. |
26
|
|
|
* |
27
|
|
|
* @return int |
28
|
|
|
*/ |
29
|
|
|
public function cacheDuration() |
30
|
|
|
{ |
31
|
|
|
if (property_exists($this, 'cacheDuration')) { |
32
|
|
|
return (int) static::$cacheDuration; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return (int) config('cacheable.duration'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return an array of method names which |
40
|
|
|
* you don't wish to be cached. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function cacheExcept() |
45
|
|
|
{ |
46
|
|
|
$result = (array) config('cacheable.except'); |
47
|
|
|
|
48
|
|
|
if (property_exists($this, 'cacheExcept')) { |
49
|
|
|
$result = array_unique(array_merge($result, (array) static::$cacheExcept)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Generate cache key. |
57
|
|
|
* |
58
|
|
|
* @param string $method |
59
|
|
|
* @param mixed $args |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function cacheKey($method, $args) |
64
|
|
|
{ |
65
|
|
|
$class = $this->baseClassName(); |
66
|
|
|
$args = sha1(serialize($args)); |
67
|
|
|
|
68
|
|
|
return implode(':', [$class, $method, $args]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the cache tags which would |
73
|
|
|
* be used by the repository. |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function cacheTags() |
78
|
|
|
{ |
79
|
|
|
if (property_exists($this, 'cacheTags')) { |
80
|
|
|
return (array) static::$cacheTags; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->model()->cacheTags(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return the primary model object which would |
88
|
|
|
* be used by the repository. |
89
|
|
|
* |
90
|
|
|
* @return \Suitmedia\Cacheable\Contracts\CacheableModel |
91
|
|
|
*/ |
92
|
|
|
abstract public function model(); |
93
|
|
|
} |
94
|
|
|
|