1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace micmania1\config; |
4
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
6
|
|
|
|
7
|
|
|
class CachedConfigCollection extends ConfigCollectionInterface |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var CachePoolInterface |
11
|
|
|
*/ |
12
|
|
|
protected $pool; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var boolean |
16
|
|
|
*/ |
17
|
|
|
protected $trackMetadata = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param boolean $trackHistory |
|
|
|
|
21
|
|
|
* @param CacheItemPoolInterface $pool |
22
|
|
|
*/ |
23
|
|
|
public function __construct(CacheItemPoolInterface $pool, $trackMetadata = false) |
24
|
|
|
{ |
25
|
|
|
$this->pool = $pool; |
|
|
|
|
26
|
|
|
$this->trackMetadata = (bool) $trackMetadata; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function set($key, ConfigItemInterface $item) |
33
|
|
|
{ |
34
|
|
|
// We use null as the key to return an empty cache item |
35
|
|
|
$cacheItem = $this->pool->getItem($key); |
36
|
|
|
|
37
|
|
|
// If we're tracking history and already have an existing record, we grab |
38
|
|
|
// that and set the new record. |
39
|
|
|
if($this->Metadata && $existing = $cacheItem->get()) { |
|
|
|
|
40
|
|
|
$existing->trackMetadata(); |
41
|
|
|
$existing->set($item->getValue(), $item->getMetadata()); |
42
|
|
|
$item = $existing; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$cacheItem->set($item); |
46
|
|
|
|
47
|
|
|
// We defer saving until later |
48
|
|
|
$this->pool->saveDeffered($cacheItem); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function get($key) |
55
|
|
|
{ |
56
|
|
|
if(!$this->exists($key)) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$cacheItem = $this->pool->getItem($key); |
61
|
|
|
|
62
|
|
|
return $cacheItem->get() ?: new ConfigItem(null, [], $this->trackHistory); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function exists($key) |
69
|
|
|
{ |
70
|
|
|
return $this->pool->hasItem($key); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function clear($key) |
77
|
|
|
{ |
78
|
|
|
$this->pool->deleteItem($key); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Commits the cache |
83
|
|
|
*/ |
84
|
|
|
public function __destruct() |
85
|
|
|
{ |
86
|
|
|
$this->pool->commit(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|