Skip to content

Commit 99ae9d6

Browse files
[Cache] Move adapter implementations to traits
1 parent 848a33e commit 99ae9d6

14 files changed

+481
-392
lines changed

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

Lines changed: 2 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,24 @@
1313

1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Log\LoggerAwareInterface;
16-
use Psr\Log\LoggerAwareTrait;
1716
use Psr\Log\LoggerInterface;
1817
use Symfony\Component\Cache\CacheItem;
1918
use Symfony\Component\Cache\Exception\InvalidArgumentException;
19+
use Symfony\Component\Cache\Traits\AbstractTrait;
2020

2121
/**
2222
* @author Nicolas Grekas <p@tchwork.com>
2323
*/
2424
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
2525
{
26-
use LoggerAwareTrait;
26+
use AbstractTrait;
2727

2828
private static $apcuSupported;
2929
private static $phpFilesSupported;
3030

31-
private $namespace;
32-
private $deferred = array();
3331
private $createCacheItem;
3432
private $mergeByLifetime;
3533

36-
/**
37-
* @var int|null The maximum length to enforce for identifiers or null when no limit applies
38-
*/
39-
protected $maxIdLength;
40-
4134
protected function __construct($namespace = '', $defaultLifetime = 0)
4235
{
4336
$this->namespace = '' === $namespace ? '' : $this->getId($namespace).':';
@@ -130,52 +123,6 @@ public static function createConnection($dsn, array $options = array())
130123
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
131124
}
132125

133-
/**
134-
* Fetches several cache items.
135-
*
136-
* @param array $ids The cache identifiers to fetch
137-
*
138-
* @return array|\Traversable The corresponding values found in the cache
139-
*/
140-
abstract protected function doFetch(array $ids);
141-
142-
/**
143-
* Confirms if the cache contains specified cache item.
144-
*
145-
* @param string $id The identifier for which to check existence
146-
*
147-
* @return bool True if item exists in the cache, false otherwise
148-
*/
149-
abstract protected function doHave($id);
150-
151-
/**
152-
* Deletes all items in the pool.
153-
*
154-
* @param string The prefix used for all identifiers managed by this pool
155-
*
156-
* @return bool True if the pool was successfully cleared, false otherwise
157-
*/
158-
abstract protected function doClear($namespace);
159-
160-
/**
161-
* Removes multiple items from the pool.
162-
*
163-
* @param array $ids An array of identifiers that should be removed from the pool
164-
*
165-
* @return bool True if the items were successfully removed, false otherwise
166-
*/
167-
abstract protected function doDelete(array $ids);
168-
169-
/**
170-
* Persists several cache items immediately.
171-
*
172-
* @param array $values The values to cache, indexed by their cache identifier
173-
* @param int $lifetime The lifetime of the cached values, 0 for persisting until manual cleaning
174-
*
175-
* @return array|bool The identifiers that failed to be cached or a boolean stating if caching succeeded or not
176-
*/
177-
abstract protected function doSave(array $values, $lifetime);
178-
179126
/**
180127
* {@inheritdoc}
181128
*/
@@ -225,87 +172,6 @@ public function getItems(array $keys = array())
225172
return $this->generateItems($items, $ids);
226173
}
227174

228-
/**
229-
* {@inheritdoc}
230-
*/
231-
public function hasItem($key)
232-
{
233-
$id = $this->getId($key);
234-
235-
if (isset($this->deferred[$key])) {
236-
$this->commit();
237-
}
238-
239-
try {
240-
return $this->doHave($id);
241-
} catch (\Exception $e) {
242-
CacheItem::log($this->logger, 'Failed to check if key "{key}" is cached', array('key' => $key, 'exception' => $e));
243-
244-
return false;
245-
}
246-
}
247-
248-
/**
249-
* {@inheritdoc}
250-
*/
251-
public function clear()
252-
{
253-
$this->deferred = array();
254-
255-
try {
256-
return $this->doClear($this->namespace);
257-
} catch (\Exception $e) {
258-
CacheItem::log($this->logger, 'Failed to clear the cache', array('exception' => $e));
259-
260-
return false;
261-
}
262-
}
263-
264-
/**
265-
* {@inheritdoc}
266-
*/
267-
public function deleteItem($key)
268-
{
269-
return $this->deleteItems(array($key));
270-
}
271-
272-
/**
273-
* {@inheritdoc}
274-
*/
275-
public function deleteItems(array $keys)
276-
{
277-
$ids = array();
278-
279-
foreach ($keys as $key) {
280-
$ids[$key] = $this->getId($key);
281-
unset($this->deferred[$key]);
282-
}
283-
284-
try {
285-
if ($this->doDelete($ids)) {
286-
return true;
287-
}
288-
} catch (\Exception $e) {
289-
}
290-
291-
$ok = true;
292-
293-
// When bulk-delete failed, retry each item individually
294-
foreach ($ids as $key => $id) {
295-
try {
296-
$e = null;
297-
if ($this->doDelete(array($id))) {
298-
continue;
299-
}
300-
} catch (\Exception $e) {
301-
}
302-
CacheItem::log($this->logger, 'Failed to delete key "{key}"', array('key' => $key, 'exception' => $e));
303-
$ok = false;
304-
}
305-
306-
return $ok;
307-
}
308-
309175
/**
310176
* {@inheritdoc}
311177
*/
@@ -394,47 +260,6 @@ public function __destruct()
394260
}
395261
}
396262

397-
/**
398-
* Like the native unserialize() function but throws an exception if anything goes wrong.
399-
*
400-
* @param string $value
401-
*
402-
* @return mixed
403-
*
404-
* @throws \Exception
405-
*/
406-
protected static function unserialize($value)
407-
{
408-
if ('b:0;' === $value) {
409-
return false;
410-
}
411-
$unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
412-
try {
413-
if (false !== $value = unserialize($value)) {
414-
return $value;
415-
}
416-
throw new \DomainException('Failed to unserialize cached value');
417-
} catch (\Error $e) {
418-
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
419-
} finally {
420-
ini_set('unserialize_callback_func', $unserializeCallbackHandler);
421-
}
422-
}
423-
424-
private function getId($key)
425-
{
426-
CacheItem::validateKey($key);
427-
428-
if (null === $this->maxIdLength) {
429-
return $this->namespace.$key;
430-
}
431-
if (strlen($id = $this->namespace.$key) > $this->maxIdLength) {
432-
$id = $this->namespace.substr_replace(base64_encode(hash('sha256', $key, true)), ':', -22);
433-
}
434-
435-
return $id;
436-
}
437-
438263
private function generateItems($items, &$keys)
439264
{
440265
$f = $this->createCacheItem;
@@ -453,12 +278,4 @@ private function generateItems($items, &$keys)
453278
yield $key => $f($key, null, false);
454279
}
455280
}
456-
457-
/**
458-
* @internal
459-
*/
460-
public static function handleUnserializeCallback($class)
461-
{
462-
throw new \DomainException('Class not found: '.$class);
463-
}
464281
}

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

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@
1313

1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Log\LoggerAwareInterface;
16-
use Psr\Log\LoggerAwareTrait;
1716
use Symfony\Component\Cache\CacheItem;
17+
use Symfony\Component\Cache\Traits\ArrayTrait;
1818

1919
/**
2020
* @author Nicolas Grekas <p@tchwork.com>
2121
*/
2222
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
2323
{
24-
use LoggerAwareTrait;
24+
use ArrayTrait;
2525

26-
private $storeSerialized;
27-
private $values = array();
28-
private $expiries = array();
2926
private $createCacheItem;
3027

3128
/**
@@ -86,49 +83,7 @@ public function getItems(array $keys = array())
8683
CacheItem::validateKey($key);
8784
}
8885

89-
return $this->generateItems($keys, time());
90-
}
91-
92-
/**
93-
* Returns all cached values, with cache miss as null.
94-
*
95-
* @return array
96-
*/
97-
public function getValues()
98-
{
99-
return $this->values;
100-
}
101-
102-
/**
103-
* {@inheritdoc}
104-
*/
105-
public function hasItem($key)
106-
{
107-
CacheItem::validateKey($key);
108-
109-
return isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
110-
}
111-
112-
/**
113-
* {@inheritdoc}
114-
*/
115-
public function clear()
116-
{
117-
$this->values = $this->expiries = array();
118-
119-
return true;
120-
}
121-
122-
/**
123-
* {@inheritdoc}
124-
*/
125-
public function deleteItem($key)
126-
{
127-
CacheItem::validateKey($key);
128-
129-
unset($this->values[$key], $this->expiries[$key]);
130-
131-
return true;
86+
return $this->generateItems($keys, time(), $this->createCacheItem);
13287
}
13388

13489
/**
@@ -196,35 +151,4 @@ public function commit()
196151
{
197152
return true;
198153
}
199-
200-
private function generateItems(array $keys, $now)
201-
{
202-
$f = $this->createCacheItem;
203-
204-
foreach ($keys as $i => $key) {
205-
try {
206-
if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= $now || !$this->deleteItem($key))) {
207-
$this->values[$key] = $value = null;
208-
} elseif (!$this->storeSerialized) {
209-
$value = $this->values[$key];
210-
} elseif ('b:0;' === $value = $this->values[$key]) {
211-
$value = false;
212-
} elseif (false === $value = unserialize($value)) {
213-
$this->values[$key] = $value = null;
214-
$isHit = false;
215-
}
216-
} catch (\Exception $e) {
217-
CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', array('key' => $key, 'exception' => $e));
218-
$this->values[$key] = $value = null;
219-
$isHit = false;
220-
}
221-
unset($keys[$i]);
222-
223-
yield $key => $f($key, $value, $isHit);
224-
}
225-
226-
foreach ($keys as $key) {
227-
yield $key => $f($key, null, false);
228-
}
229-
}
230154
}

0 commit comments

Comments
 (0)