Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3072ecf

Browse files
[Cache] Allow and use generators in AbstractAdapter
1 parent cc84be9 commit 3072ecf

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

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

Lines changed: 40 additions & 19 deletions
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,41 @@ 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+
static $prefix = "\0Symfony\Component\Cache\CacheItem\0";
166+
$item = (array) $this->deferred[$key];
167+
$ok = $this->doSave(array($item[$prefix.'key'] => $item[$prefix.'value']), $item[$prefix.'lifetime']);
168+
unset($this->deferred[$key]);
169+
170+
if (true === $ok || array() === $ok) {
171+
return true;
172+
}
170173
}
171174

172-
return $this->doHave($this->getId($key));
175+
return $this->doHave($id);
173176
}
174177

175178
/**
@@ -179,7 +182,7 @@ public function clear()
179182
{
180183
$this->deferred = array();
181184

182-
return $this->doClear();
185+
return $this->doClear($this->namespace);
183186
}
184187

185188
/**
@@ -289,4 +292,22 @@ private function getId($key)
289292

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

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/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: 1 addition & 4 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
/**

0 commit comments

Comments
 (0)