1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace micmania1\config; |
4
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
6
|
|
|
|
7
|
|
|
class CachedConfigCollection implements ConfigCollectionInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @const string |
11
|
|
|
*/ |
12
|
|
|
const METADATA_KEY = '__METADATA__'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @const string |
16
|
|
|
*/ |
17
|
|
|
const HISTORY_KEY = '__HISTORY__'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var CacheItemPoolInterface |
21
|
|
|
*/ |
22
|
|
|
protected $pool; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var boolean |
26
|
|
|
*/ |
27
|
|
|
protected $trackMetadata = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param boolean $trackMetadata |
31
|
|
|
* @param CacheItemPoolInterface $pool |
32
|
|
|
*/ |
33
|
4 |
|
public function __construct(CacheItemPoolInterface $pool, $trackMetadata = false) |
34
|
|
|
{ |
35
|
4 |
|
$this->pool = $pool; |
36
|
4 |
|
$this->trackMetadata = (bool) $trackMetadata; |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
3 |
|
public function set($key, $value, $metadata = []) |
43
|
|
|
{ |
44
|
|
|
// We use null as the key to return an empty cache item |
45
|
3 |
|
$cacheItem = $this->pool->getItem($key); |
46
|
|
|
|
47
|
3 |
|
if($this->trackMetadata) { |
48
|
1 |
|
$cachedMetadata = $this->getMetadata(); |
49
|
1 |
|
$cachedHistory = $this->getHistory(); |
50
|
|
|
|
51
|
1 |
|
if($this->exists($key) && isset($metadata[$key])) { |
52
|
|
|
array_unshift($cachedHistory, [ |
53
|
|
|
'value' => $value, |
54
|
|
|
'metadata' => $metadata, |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$cachedMetadata[$key] = $metadata; |
59
|
|
|
|
60
|
1 |
|
$this->saveMetadata($cachedMetadata); |
61
|
1 |
|
$this->saveHistory($cachedHistory); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
// Save our new value |
65
|
3 |
|
$cacheItem->set($value); |
66
|
3 |
|
$this->pool->saveDeferred($cacheItem); |
67
|
3 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function get($key) |
73
|
|
|
{ |
74
|
1 |
|
if(!$this->exists($key)) { |
75
|
1 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$cacheItem = $this->pool->getItem($key); |
79
|
|
|
|
80
|
1 |
|
return $cacheItem->get() ?: null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
2 |
|
public function exists($key) |
87
|
|
|
{ |
88
|
2 |
|
return $this->pool->hasItem($key); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function delete($key) |
95
|
|
|
{ |
96
|
1 |
|
$this->pool->deleteItem($key); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
public function deleteAll() |
103
|
|
|
{ |
104
|
1 |
|
$this->pool->clear(); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
2 |
|
public function getMetadata() |
111
|
|
|
{ |
112
|
2 |
|
return $this->getTrackingData(self::METADATA_KEY); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
2 |
|
public function getHistory() |
119
|
|
|
{ |
120
|
2 |
|
return $this->getTrackingData(self::HISTORY_KEY); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* A shortcut for tracking data (metadata and history). This will |
125
|
|
|
* always return an array, even if we're not tracking. |
126
|
|
|
* |
127
|
|
|
* @param string $key |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
2 |
|
private function getTrackingData($key) |
132
|
|
|
{ |
133
|
2 |
|
if (!$this->trackMetadata) { |
134
|
1 |
|
return []; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$value = $this->pool->getItem($key)->get(); |
138
|
|
|
|
139
|
1 |
|
return is_array($value) ? $value : []; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Saves the metadata to cache |
144
|
|
|
* |
145
|
|
|
* @param array $metadata |
146
|
|
|
*/ |
147
|
1 |
|
protected function saveMetadata($metadata) |
148
|
|
|
{ |
149
|
1 |
|
$cached = $this->pool->getItem(self::METADATA_KEY); |
150
|
1 |
|
$cached->set($metadata); |
151
|
|
|
|
152
|
1 |
|
$this->pool->saveDeferred($cached); |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Saves the history to the cache |
157
|
|
|
* |
158
|
|
|
* @param array $history |
159
|
|
|
*/ |
160
|
1 |
|
protected function saveHistory($history) |
161
|
|
|
{ |
162
|
1 |
|
$cached = $this->pool->getItem(self::HISTORY_KEY); |
163
|
1 |
|
$cached->set($history); |
164
|
|
|
|
165
|
1 |
|
$this->pool->saveDeferred($cached); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Commits the cache |
170
|
|
|
*/ |
171
|
4 |
|
public function __destruct() |
172
|
|
|
{ |
173
|
4 |
|
$this->pool->commit(); |
174
|
4 |
|
} |
175
|
|
|
} |
176
|
|
|
|