1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix\Cache\PsrCache; |
14
|
|
|
|
15
|
|
|
use Apix\Cache\Adapter as CacheAdapter; |
16
|
|
|
use Psr\Cache\CacheItemInterface as ItemInterface; |
17
|
|
|
use Psr\Cache\CacheItemPoolInterface as ItemPoolInterface; |
18
|
|
|
|
19
|
|
|
class Pool implements ItemPoolInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var CacheAdapter |
25
|
|
|
*/ |
26
|
|
|
protected $cache_adapter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Deferred cache items to be saved later. |
30
|
|
|
* |
31
|
|
|
* @var array Collection of \Apix\PsrCache\Item. |
32
|
|
|
*/ |
33
|
|
|
protected $deferred = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
*/ |
38
|
374 |
View Code Duplication |
public function __construct(CacheAdapter $cache_adapter) |
|
|
|
|
39
|
|
|
{ |
40
|
374 |
|
$this->cache_adapter = $cache_adapter; |
41
|
|
|
|
42
|
|
|
$options = array( |
43
|
|
|
'tag_enable' => false // wether to enable tagging |
44
|
374 |
|
); |
45
|
374 |
|
$this->cache_adapter->setOptions($options); |
46
|
374 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
170 |
|
public function getItem($key) |
52
|
|
|
{ |
53
|
170 |
|
$key = Item::normalizedKey($key); |
54
|
|
|
|
55
|
170 |
|
if (isset($this->deferred[$key])) { |
56
|
170 |
|
return $this->deferred[$key]; |
57
|
170 |
|
} |
58
|
170 |
|
|
59
|
|
|
$value = $this->cache_adapter->loadKey($key); |
60
|
170 |
|
|
61
|
|
|
return new Item( |
62
|
|
|
$this->cache_adapter->removePrefixKey($key), |
63
|
|
|
$value, |
64
|
|
|
$this->cache_adapter->getTtl($key) ?: null, |
65
|
|
|
(bool) $value // indicates wether it is loaded from cache or not. |
66
|
68 |
|
); |
67
|
|
|
} |
68
|
68 |
|
|
69
|
68 |
|
/** |
70
|
68 |
|
* {@inheritdoc} |
71
|
68 |
|
*/ |
72
|
|
|
public function getItems(array $keys = array()) |
73
|
68 |
|
{ |
74
|
|
|
$items = array(); |
75
|
|
|
foreach ($keys as $key) { |
76
|
|
|
$items[$key] = $this->getItem($key); |
77
|
|
|
} |
78
|
|
|
|
79
|
68 |
|
return $items; |
80
|
|
|
} |
81
|
68 |
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function hasItem($key) |
86
|
|
|
{ |
87
|
34 |
|
return $this->getItem($key)->isHit(); |
88
|
|
|
} |
89
|
34 |
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function clear() |
94
|
|
|
{ |
95
|
68 |
|
$this->deferred = array(); |
96
|
|
|
return $this->cache_adapter->flush(true); |
97
|
68 |
|
} |
98
|
68 |
|
|
99
|
68 |
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
68 |
|
*/ |
102
|
|
|
public function deleteItems(array $keys) |
103
|
|
|
{ |
104
|
|
|
$checks = array(); |
105
|
|
|
foreach ($keys as $key) { |
106
|
|
|
// Only delete from cache if it actually exists |
107
|
34 |
|
if($this->getItem($key)->isHit()) { |
108
|
|
|
$checks[] = $this->cache_adapter->delete($key); |
109
|
34 |
|
} |
110
|
|
|
unset($this->deferred[$key]); |
111
|
|
|
} |
112
|
|
|
return (bool) !in_array(false, $checks, true); |
113
|
|
|
} |
114
|
|
|
|
115
|
170 |
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
170 |
|
*/ |
118
|
|
|
public function deleteItem($key) |
119
|
170 |
|
{ |
120
|
170 |
|
return $this->deleteItems(array($key)); |
121
|
170 |
|
} |
122
|
170 |
|
|
123
|
170 |
|
/** |
124
|
170 |
|
* {@inheritdoc} |
125
|
170 |
|
*/ |
126
|
170 |
|
public function save(ItemInterface $item) |
127
|
|
|
{ |
128
|
170 |
|
$ttl = $item->getTtlInSecond(); |
129
|
|
|
|
130
|
|
|
$item->setHit(true); |
131
|
|
|
$success = $this->cache_adapter->save( |
132
|
|
|
$item->get(), // value to store |
133
|
|
|
$item->getKey(), // its key |
134
|
34 |
|
null, // disable tags support |
135
|
|
|
is_null($ttl) ? 0 : $ttl // ttl in sec or null for ever |
136
|
34 |
|
); |
137
|
|
|
$item->setHit($success); |
138
|
34 |
|
|
139
|
|
|
return $success; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
34 |
|
*/ |
145
|
|
|
public function saveDeferred(ItemInterface $item) |
146
|
34 |
|
{ |
147
|
34 |
|
$this->deferred[$item->getKey()] = $item; |
148
|
34 |
|
|
149
|
34 |
|
return $this; |
|
|
|
|
150
|
34 |
|
} |
151
|
34 |
|
|
152
|
|
|
/** |
153
|
34 |
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function commit() |
156
|
|
|
{ |
157
|
|
|
foreach ($this->deferred as $key => $item) { |
158
|
|
|
$this->save($item); |
159
|
|
|
if ( $item->isHit() ) { |
160
|
|
|
unset($this->deferred[$key]); |
161
|
289 |
|
} |
162
|
|
|
} |
163
|
289 |
|
|
164
|
|
|
return empty($this->deferred); |
165
|
|
|
} |
166
|
|
|
|
167
|
17 |
|
/** |
168
|
|
|
* Returns the cache adapter for this pool. |
169
|
|
|
* |
170
|
|
|
* @return CacheAdapter |
171
|
|
|
*/ |
172
|
|
|
public function getCacheAdapter() |
173
|
|
|
{ |
174
|
|
|
return $this->cache_adapter; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Commit the deferred items ~ acts as the last resort garbage collector. |
179
|
|
|
*/ |
180
|
|
|
public function __destruct() |
181
|
|
|
{ |
182
|
|
|
$this->commit(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.