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

Skip to content

Commit 9d8c6c6

Browse files
committed
feature #20694 [Cache] Implement PSR-16 SimpleCache v1.0 (nicolas-grekas)
This PR was squashed before being merged into the 3.3-dev branch (closes #20694). Discussion ---------- [Cache] Implement PSR-16 SimpleCache v1.0 | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | not yet | Fixed tickets | - | License | MIT | Doc PR | symfony/symfony-docs#7409 Second iteration on the topic after #20636 raised some issues. Please don't squash while merging. Commits ------- 6219dd6 [Cache] Create PSR-16 variants of all PSR-6 adapters 99ae9d6 [Cache] Move adapter implementations to traits 848a33e [Cache] Implement PSR-16 SimpleCache v1.0
2 parents 3f8aa7b + 6219dd6 commit 9d8c6c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+4641
-1607
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"twig/twig": "~1.28|~2.0",
2222
"psr/cache": "~1.0",
2323
"psr/log": "~1.0",
24+
"psr/simple-cache": "^1.0",
2425
"symfony/polyfill-intl-icu": "~1.0",
2526
"symfony/polyfill-mbstring": "~1.0",
2627
"symfony/polyfill-php56": "~1.0",
@@ -79,7 +80,7 @@
7980
"symfony/yaml": "self.version"
8081
},
8182
"require-dev": {
82-
"cache/integration-tests": "dev-master",
83+
"cache/integration-tests": "^0.15.0",
8384
"doctrine/cache": "~1.6",
8485
"doctrine/data-fixtures": "1.0.*",
8586
"doctrine/dbal": "~2.4",
@@ -99,7 +100,8 @@
99100
"phpdocumentor/type-resolver": "<0.2.0"
100101
},
101102
"provide": {
102-
"psr/cache-implementation": "1.0"
103+
"psr/cache-implementation": "1.0",
104+
"psr/simple-cache-implementation": "1.0"
103105
},
104106
"autoload": {
105107
"psr-4": {

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
<array>
6363
<element><string>Cache\IntegrationTests</string></element>
6464
<element><string>Doctrine\Common\Cache</string></element>
65+
<element><string>Symfony\Component\Cache</string></element>
66+
<element><string>Symfony\Component\Cache\Traits</string></element>
6567
<element><string>Symfony\Component\Console</string></element>
6668
<element><string>Symfony\Component\HttpFoundation</string></element>
6769
</array>

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 <[email protected]>
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/ApcuAdapter.php

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,97 +11,14 @@
1111

1212
namespace Symfony\Component\Cache\Adapter;
1313

14-
use Symfony\Component\Cache\CacheItem;
15-
use Symfony\Component\Cache\Exception\CacheException;
14+
use Symfony\Component\Cache\Traits\ApcuTrait;
1615

17-
/**
18-
* @author Nicolas Grekas <[email protected]>
19-
*/
2016
class ApcuAdapter extends AbstractAdapter
2117
{
22-
public static function isSupported()
23-
{
24-
return function_exists('apcu_fetch') && ini_get('apc.enabled') && !('cli' === PHP_SAPI && !ini_get('apc.enable_cli'));
25-
}
18+
use ApcuTrait;
2619

2720
public function __construct($namespace = '', $defaultLifetime = 0, $version = null)
2821
{
29-
if (!static::isSupported()) {
30-
throw new CacheException('APCu is not enabled');
31-
}
32-
if ('cli' === PHP_SAPI) {
33-
ini_set('apc.use_request_time', 0);
34-
}
35-
parent::__construct($namespace, $defaultLifetime);
36-
37-
if (null !== $version) {
38-
CacheItem::validateKey($version);
39-
40-
if (!apcu_exists($version.'@'.$namespace)) {
41-
$this->clear($namespace);
42-
apcu_add($version.'@'.$namespace, null);
43-
}
44-
}
45-
}
46-
47-
/**
48-
* {@inheritdoc}
49-
*/
50-
protected function doFetch(array $ids)
51-
{
52-
try {
53-
return apcu_fetch($ids);
54-
} catch (\Error $e) {
55-
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
56-
}
57-
}
58-
59-
/**
60-
* {@inheritdoc}
61-
*/
62-
protected function doHave($id)
63-
{
64-
return apcu_exists($id);
65-
}
66-
67-
/**
68-
* {@inheritdoc}
69-
*/
70-
protected function doClear($namespace)
71-
{
72-
return isset($namespace[0]) && class_exists('APCuIterator', false)
73-
? apcu_delete(new \APCuIterator(sprintf('/^%s/', preg_quote($namespace, '/')), APC_ITER_KEY))
74-
: apcu_clear_cache();
75-
}
76-
77-
/**
78-
* {@inheritdoc}
79-
*/
80-
protected function doDelete(array $ids)
81-
{
82-
foreach ($ids as $id) {
83-
apcu_delete($id);
84-
}
85-
86-
return true;
87-
}
88-
89-
/**
90-
* {@inheritdoc}
91-
*/
92-
protected function doSave(array $values, $lifetime)
93-
{
94-
try {
95-
return array_keys(apcu_store($values, null, $lifetime));
96-
} catch (\Error $e) {
97-
} catch (\Exception $e) {
98-
}
99-
100-
if (1 === count($values)) {
101-
// Workaround https://github.com/krakjoe/apcu/issues/170
102-
apcu_delete(key($values));
103-
}
104-
105-
throw $e;
22+
$this->init($namespace, $defaultLifetime, $version);
10623
}
10724
}

0 commit comments

Comments
 (0)