8000 [Cache] Allow and use generators in AbstractAdapter · symfony/symfony@2bc3bfa · GitHub
[go: up one dir, main page]

Skip to content

Commit 2bc3bfa

Browse files
[Cache] Allow and use generators in AbstractAdapter
1 parent cc84be9 commit 2bc3bfa

File tree

6 files changed

+53
-33
lines changed

6 files changed

+53
-33
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 39 additions & 19 deletions
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function ($deferred, $namespace) {
6464
*
6565
* @param array $ids The cache identifiers to fetch.
6666
*
67-
* @return array The corresponding values found in the cache.
67+
* @return array|\Traversable The corresponding values found in the cache.
6868
*/
6969
abstract protected function doFetch(array $ids);
7070

@@ -80,9 +80,11 @@ abstract protected function doHave($id);
8080
/**
8181
* Deletes all items in the pool.
8282
*
83+
* @param string The prefix used for all identifiers managed by this pool.
84+
*
8385
* @return bool True if the pool was successfully cleared, false otherwise.
8486
*/
85-
abstract protected function doClear();
87+
abstract protected function doClear($namespace);
8688

8789
/**
8890
* Removes multiple items from the pool.
@@ -114,7 +116,7 @@ public function getItem($key)
114116
$this->commit();
115117
}
116118
if (isset($this->deferred[$key])) {
117-
return $this->deferred[$key];
119+
return clone $this->deferred[$key];
118120
}
119121

120122
$f = $this->createCacheItem;
@@ -136,40 +138,40 @@ public function getItems(array $keys = array())
136138
if ($this->deferred) {
137139
$this->commit();
138140
}
139-
$f = $this->createCacheItem;
140141
$ids = array();
141-
$items = array();
142+
$deferred = array();
142143

143144
foreach ($keys as $key) {
144145
$id = $this->getId($key);
145146

146147
if (isset($this->deferred[$key])) {
147-
$items[$key] = $this->deferred[$key];
148+
$deferred[] = $key;
148149
} else {
149-
$ids[$key] = $id;
150+
$ids[$id] = $key;
150151
}
151152
}
152153

153-
$values = $this->doFetch($ids);
154-
155-
foreach ($ids as $key => $id) {
156-
$isHit = isset($values[$id]);
157-
$items[$key] = $f($key, $isHit ? $values[$id] : null, $isHit);
158-
}
159-
160-
return $items;
154+
return $this->generateItems($deferred, $ids);
161155
}
162156

163157
/**
164158
* {@inheritdoc}
165159
*/
166160
public function hasItem($key)
167161
{
168-
if ($this->deferred) {
169-
$this->commit();
162+
$id = $this->getId($key);
163+
164+
if (isset($this->deferred[$key])) {
165+
$item = (array) $this->deferred[$key];
166+
$ok = $this->doSave(array($item[CacheItem::CAST_PREFIX.'key'] => $item[CacheItem::CAST_PREFIX.'value']), $item[CacheItem::CAST_PREFIX.'lifetime']);
167+
unset($this->deferred[$key]);
168+
169+
if (true === $ok || array() === $ok) {
170+
return true;
171+
}
170172
}
171173

172-
return $this->doHave($this->getId($key));
174+
return $this->doHave($id);
173175
}
174176

175177
/**
@@ -179,7 +181,7 @@ public function clear()
179181
{
180182
$this->deferred = array();
181183

182-
return $this->doClear();
184+
return $this->doClear($this->namespace);
183185
}
184186

185187
/**
@@ -289,4 +291,22 @@ private function getId($key)
289291

290292
return $this->namespace.$key;
291293
}
294+
295+
private function generateItems($deferred, $ids)
296+
{
297+
foreach ($deferred as $key) {
298+
yield $key => clone $this->deferred[$key];
299+
}
300+
301+
foreach ($this->doFetch(array_keys($ids)) as $id => $value) {
302+
if (isset($ids[$id])) {
303+
yield $ids[$id] => $f($ids[$id], $value, true);
304+
unset($ids[$id]);
305+
}
306+
}
307+
308+
foreach ($ids as $id => $key) {
309+
yield $key => $f($key, null, false);
310+
}
311+
}
292312
}

src/Symfony/Component/Cache/Adapter/ApcuAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function doHave($id)
4848
/**
4949
* {@inheritdoc}
5050
*/
51-
protected function doClear()
51+
protected function doClear($namespace)
5252
{
5353
return apcu_clear_cache();
5454
}

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,10 @@ public function save(CacheItemInterface $item)
118118
if (!$item instanceof CacheItem) {
119119
return false;
120120
}
121-
static $prefix = "\0Symfony\Component\Cache\CacheItem\0";
122121
$item = (array) $item;
123-
$key = $item[$prefix.'key'];
124-
$value = $item[$prefix.'value'];
125-
$lifetime = $item[$prefix.'lifetime'];
122+
$key = $item[CacheItem::CAST_PREFIX.'key'];
123+
$value = $item[CacheItem::CAST_PREFIX.'value'];
124+
$lifetime = $item[CacheItem::CAST_PREFIX.'lifetime'];
126125

127126
if (0 > $lifetime) {
128127
return true;

src/Symfony/Component/Cache/Adapter/DoctrineAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function doHave($id)
4545
/**
4646
* {@inheritdoc}
4747
*/
48-
protected function doClear()
48+
protected function doClear($namespace)
4949
{
5050
return $this->provider->flushAll();
5151
}

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,10 @@ public function getItem($key)
5757
public function getItems(array $keys = array())
5858
{
5959
$f = $this->createCacheItem;
60-
$items = array();
6160

6261
foreach ($this->pool->getItems($keys) as $key => $item) {
63-
$items[$key] = $f($key, $item->get(), $item->isHit());
62+
yield $key => $f($key, $item->get(), $item->isHit());
6463
}
65-
66-
return $items;
6764
}
6865

6966
/**
@@ -127,11 +124,10 @@ private function doSave(CacheItemInterface $item, $method)
127124
if (!$item instanceof CacheItem) {
128125
return false;
129126
}
130-
static $prefix = "\0Symfony\Component\Cache\CacheItem\0";
131127
$item = (array) $item;
132-
$poolItem = $this->pool->getItem($item[$prefix.'key']);
133-
$poolItem->set($item[$prefix.'value']);
134-
$poolItem->expiresAfter($item[$prefix.'lifetime']);
128+
$poolItem = $this->pool->getItem($item[CacheItem::CAST_PREFIX.'key']);
129+
$poolItem->set($item[CacheItem::CAST_PREFIX.'value']);
130+
$poolItem->expiresAfter($item[CacheItem::CAST_PREFIX.'lifetime']);
135131

136132
return $this->pool->$method($poolItem);
137133
}

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
*/
2020
final class CacheItem implements CacheItemInterface
2121
{
22+
/**
23+
* @internal
24+
*/
25+
const CAST_PREFIX = "\0Symfony\Component\Cache\CacheItem\0";
26+
2227
private $key;
2328
private $value;
2429
private $isHit;

0 commit comments

Comments
 (0)
0