1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Cache; |
4
|
|
|
|
5
|
|
|
use Isswp101\Persimmon\Model; |
6
|
|
|
|
7
|
|
|
class RuntimeCache |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Cache. |
11
|
|
|
* |
12
|
|
|
* @var array [ |
13
|
|
|
* 'instance' => Model, |
14
|
|
|
* 'attributes' => [] |
15
|
|
|
* ] |
16
|
|
|
*/ |
17
|
|
|
private $cache = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Return true if cache contains this key. |
21
|
|
|
* |
22
|
|
|
* @param mixed $key |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
22 |
|
public function has($key) |
26
|
|
|
{ |
27
|
22 |
|
return array_key_exists($key, $this->cache); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Return instance from cache. |
32
|
|
|
* |
33
|
|
|
* @param mixed $key |
34
|
|
|
* @return Model |
35
|
|
|
*/ |
36
|
10 |
|
public function get($key) |
37
|
|
|
{ |
38
|
10 |
|
return $this->has($key) ? $this->cache[$key]['instance'] : null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Return all cache keys. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function keys() |
47
|
|
|
{ |
48
|
|
|
return array_keys($this->cache); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Put instance to cache. |
53
|
|
|
* |
54
|
|
|
* @param mixed $key |
55
|
|
|
* @param Model $instance |
56
|
|
|
* @param array $attributes |
57
|
|
|
* @return Model |
58
|
|
|
*/ |
59
|
10 |
|
public function put($key, Model $instance, array $attributes = ['*']) |
60
|
|
|
{ |
61
|
10 |
|
if ($attributes != ['*'] && $this->has($key)) { |
62
|
|
|
$instance = Model::merge($this->cache[$key]['instance'], $instance, $attributes); |
63
|
|
|
$attributes = array_merge($this->cache[$key]['attributes'], $attributes); |
64
|
|
|
} |
65
|
|
|
|
66
|
10 |
|
$this->cache[$key] = [ |
67
|
10 |
|
'instance' => $instance, |
68
|
|
|
'attributes' => $attributes |
69
|
10 |
|
]; |
70
|
|
|
|
71
|
10 |
|
return $instance; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return true if cache has already given attributes. |
76
|
|
|
* |
77
|
|
|
* @param mixed $key |
78
|
|
|
* @param array $attributes |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
22 |
|
public function containsAttributes($key, array $attributes = ['*']) |
82
|
|
|
{ |
83
|
22 |
|
return empty($this->getNotCachedAttributes($key, $attributes)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return the difference between given attributes and attributes which are already cached. |
88
|
|
|
* |
89
|
|
|
* @param mixed $key |
90
|
|
|
* @param array $attributes |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
22 |
|
public function getNotCachedAttributes($key, array $attributes = ['*']) |
94
|
|
|
{ |
95
|
22 |
|
if (!$this->has($key)) { |
96
|
12 |
|
return $attributes; |
97
|
|
|
} |
98
|
12 |
|
$cached_attributes = $this->cache[$key]['attributes']; |
99
|
12 |
|
return $cached_attributes == ['*'] ? [] : array_diff($attributes, $cached_attributes); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Remove an item from the cache by key. |
104
|
|
|
* |
105
|
|
|
* @param mixed $key |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
6 |
|
public function forget($key) |
109
|
|
|
{ |
110
|
6 |
|
unset($this->cache[$key]); |
111
|
6 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|