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