1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpAbac\Cache\Pool; |
4
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
6
|
|
|
use Psr\Cache\CacheItemInterface; |
7
|
|
|
|
8
|
|
|
use PhpAbac\Cache\Item\MemoryCacheItem; |
9
|
|
|
|
10
|
|
|
class MemoryCacheItemPool implements CacheItemPoolInterface { |
11
|
|
|
/** @var array **/ |
12
|
|
|
protected $items; |
13
|
|
|
/** @var array **/ |
14
|
|
|
protected $deferredItems; |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
2 |
|
public function deleteItem($key) { |
19
|
2 |
|
if(isset($this->items[$key])) { |
|
|
|
|
20
|
2 |
|
unset($this->items[$key]); |
21
|
2 |
|
} |
22
|
2 |
|
if(isset($this->deferredItems[$key])) { |
|
|
|
|
23
|
|
|
unset($this->deferredItems[$key]); |
24
|
|
|
} |
25
|
2 |
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
1 |
|
public function deleteItems(array $keys) { |
32
|
1 |
|
foreach($keys as $key) { |
|
|
|
|
33
|
1 |
|
if(!$this->deleteItem($key)) { |
|
|
|
|
34
|
|
|
return false; |
35
|
|
|
} |
36
|
1 |
|
} |
37
|
1 |
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
9 |
|
public function save(CacheItemInterface $item) { |
44
|
9 |
|
$this->items[$item->getKey()] = $item; |
45
|
|
|
|
46
|
9 |
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
2 |
|
public function saveDeferred(CacheItemInterface $item) { |
53
|
2 |
|
$this->deferredItems[$item->getKey()] = $item; |
54
|
|
|
|
55
|
2 |
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
2 |
|
public function commit() { |
62
|
2 |
|
foreach($this->deferredItems as $key => $item) { |
|
|
|
|
63
|
2 |
|
$this->items[$key] = $item; |
64
|
2 |
|
unset($this->deferredItems[$key]); |
65
|
2 |
|
} |
66
|
2 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
13 |
|
public function hasItem($key) { |
73
|
13 |
|
return isset($this->items[$key]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
9 |
|
public function getItem($key) { |
80
|
9 |
|
if(!$this->hasItem($key)) { |
|
|
|
|
81
|
4 |
|
return new MemoryCacheItem($key); |
82
|
|
|
} |
83
|
7 |
|
return $this->items[$key]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
3 |
|
public function getItems(array $keys = array()) { |
90
|
3 |
|
$items = []; |
91
|
3 |
|
foreach($keys as $key) { |
|
|
|
|
92
|
3 |
|
if($this->hasItem($key)) { |
|
|
|
|
93
|
3 |
|
$items[$key] = $this->getItem($key); |
94
|
3 |
|
} |
95
|
3 |
|
} |
96
|
3 |
|
return $items; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
public function clear() { |
103
|
1 |
|
$this->items = []; |
104
|
1 |
|
$this->deferredItems = []; |
105
|
1 |
|
return true; |
106
|
|
|
} |
107
|
|
|
} |