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

Skip to content

Commit 848a33e

Browse files
[Cache] Implement PSR-16 SimpleCache v1.0
1 parent 60d7d43 commit 848a33e

File tree

9 files changed

+347
-10
lines changed

9 files changed

+347
-10
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>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Adapter;
13+
14+
use Psr\SimpleCache\CacheInterface;
15+
16+
/**
17+
* @author Nicolas Grekas <[email protected]>
18+
*/
19+
class SimpleCacheAdapter extends AbstractAdapter
20+
{
21+
private $pool;
22+
private $miss;
23+
24+
public function __construct(CacheInterface $pool, $namespace = '', $defaultLifetime = 0)
25+
{
26+
parent::__construct($namespace, $defaultLifetime);
27+
28+
$this->pool = $pool;
29+
$this->miss = new \stdClass();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
protected function doFetch(array $ids)
36+
{
37+
foreach ($this->pool->getMultiple($ids, $this->miss) as $key => $value) {
38+
if ($this->miss !== $value) {
39+
yield $key => $value;
40+
}
41+
}
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
protected function doHave($id)
48+
{
49+
return $this->pool->has($id);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected function doClear($namespace)
56+
{
57+
return $this->pool->clear();
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
protected function doDelete(array $ids)
64+
{
65+
return $this->pool->deleteMultiple($ids);
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
protected function doSave(array $values, $lifetime)
72+
{
73+
return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
74+
}
75+
}

src/Symfony/Component/Cache/Exception/CacheException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Symfony\Component\Cache\Exception;
1313

14-
use Psr\Cache\CacheException as CacheExceptionInterface;
14+
use Psr\Cache\CacheException as Psr6CacheInterface;
15+
use Psr\SimpleCache\CacheException as SimpleCacheInterface;
1516

16-
class CacheException extends \Exception implements CacheExceptionInterface
17+
class CacheException extends \Exception implements Psr6CacheInterface, SimpleCacheInterface
1718
{
1819
}

src/Symfony/Component/Cache/Exception/InvalidArgumentException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Symfony\Component\Cache\Exception;
1313

14-
use Psr\Cache\InvalidArgumentException as InvalidArgumentExceptionInterface;
14+
use Psr\Cache\InvalidArgumentException as Psr6CacheInterface;
15+
use Psr\SimpleCache\InvalidArgumentException as SimpleCacheInterface;
1516

16-
class InvalidArgumentException extends \InvalidArgumentException implements InvalidArgumentExceptionInterface
17+
class InvalidArgumentException extends \InvalidArgumentException implements Psr6CacheInterface, SimpleCacheInterface
1718
{
1819
}
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Simple;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Psr\Cache\CacheException as Psr6CacheException;
16+
use Psr\SimpleCache\CacheInterface;
17+
use Psr\SimpleCache\CacheException as SimpleCacheException;
18+
use Symfony\Component\Cache\CacheItem;
19+
use Symfony\Component\Cache\Exception\InvalidArgumentException;
20+
21+
/**
22+
* @author Nicolas Grekas <[email protected]>
23+
*/
24+
class Psr6Cache implements CacheInterface
25+
{
26+
private $pool;
27+
private $createCacheItem;
28+
29+
public function __construct(CacheItemPoolInterface $pool)
30+
{
31+
$this->pool = $pool;
32+
33+
if ($pool instanceof Adapter\AdapterInterface) {
34+
$this->createCacheItem = \Closure::bind(
35+
function ($key, $value, $allowInt = false) {
36+
if ($allowInt && is_int($key)) {
37+
$key = (string) $key;
38+
} else {
39+
CacheItem::validateKey($key);
40+
}
41+
$item = new CacheItem();
42+
$item->key = $key;
43+
$item->value = $value;
44+
45+
return $item;
46+
},
47+
null,
48+
CacheItem::class
49+
);
50+
}
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function get($key, $default = null)
57+
{
58+
try {
59+
$item = $this->pool->getItem($key);
60+
} catch (SimpleCacheException $e) {
61+
throw $e;
62+
} catch (Psr6CacheException $e) {
63+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
64+
}
65+
66+
return $item->isHit() ? $item->get() : $default;
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public function set($key, $value, $ttl = null)
73+
{
74+
try {
75+
if (null !== $f = $this->createCacheItem) {
76+
$item = $f($key, $value);
77+
} else {
78+
$item = $this->pool->getItem($key)->set($value);
79+
}
80+
} catch (SimpleCacheException $e) {
81+
throw $e;
82+
} catch (Psr6CacheException $e) {
83+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
84+
}
85+
if (null !== $ttl) {
86+
$item->expiresAfter($ttl);
87+
}
88+
89+
return $this->pool->save($item);
90+
}
91+
92+
/**
93+
* {@inheritdoc}
94+
*/
95+
public function delete($key)
96+
{
97+
try {
98+
return $this->pool->deleteItem($key);
99+
} catch (SimpleCacheException $e) {
100+
throw $e;
101+
} catch (Psr6CacheException $e) {
102+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
103+
}
104+
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function clear()
110+
{
111+
return $this->pool->clear();
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public function getMultiple($keys, $default = null)
118+
{
119+
if ($keys instanceof \Traversable) {
120+
$keys = iterator_to_array($keys, false);
121+
} elseif (!is_array($keys)) {
122+
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
123+
}
124+
125+
try {
126+
$items = $this->pool->getItems($keys);
127+
} catch (SimpleCacheException $e) {
128+
throw $e;
129+
} catch (Psr6CacheException $e) {
130+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
131+
}
132+
$values = array();
133+
134+
foreach ($items as $key => $item) {
135+
$values[$key] = $item->isHit() ? $item->get() : $default;
136+
}
137+
138+
return $values;
139+
}
140+
141+
/**
142+
* {@inheritdoc}
143+
*/
144+
public function setMultiple($values, $ttl = null)
145+
{
146+
$valuesIsArray = is_array($values);
147+
if (!$valuesIsArray && !$values instanceof \Traversable) {
148+
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values)));
149+
}
150+
$items = array();
151+
152+
try {
153+
if (null !== $f = $this->createCacheItem) {
154+
$valuesIsArray = false;
155+
foreach ($values as $key => $value) {
156+
$items[$key] = $f($key, $value, true);
157+
}
158+
} elseif ($valuesIsArray) {
159+
$items = array();
160+
foreach ($values as $key => $value) {
161+
$items[] = (string) $key;
162+
}
163+
$items = $this->pool->getItems($items);
164+
} else {
165+
foreach ($values as $key => $value) {
166+
if (is_int($key)) {
167+
$key = (string) $key;
168+
}
169+
$items[$key] = $this->pool->getItem($key)->set($value);
170+
}
171+
}
172+
} catch (SimpleCacheException $e) {
173+
throw $e;
174+
} catch (Psr6CacheException $e) {
175+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
176+
}
177+
$ok = true;
178+
179+
foreach ($items as $key => $item) {
180+
if ($valuesIsArray) {
181+
$item->set($values[$key]);
182+
}
183+
if (null !== $ttl) {
184+
$item->expiresAfter($ttl);
185+
}
186+
$ok = $this->pool->saveDeferred($item) && $ok;
187+
}
188+
189+
return $this->pool->commit() && $ok;
190+
}
191+
192+
/**
193+
* {@inheritdoc}
194+
*/
195+
public function deleteMultiple($keys)
196+
{
197+
if ($keys instanceof \Traversable) {
198+
$keys = iterator_to_array($keys, false);
199+
} elseif (!is_array($keys)) {
200+
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
201+
}
202+
203+
try {
204+
return $this->pool->deleteItems($keys);
205+
} catch (SimpleCacheException $e) {
206+
throw $e;
207+
} catch (Psr6CacheException $e) {
208+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
209+
}
210+
}
211+
212+
/**
213+
* {@inheritdoc}
214+
*/
215+
public function has($key)
216+
{
217+
try {
218+
return $this->pool->hasItem($key);
219+
} catch (SimpleCacheException $e) {
220+
throw $e;
221+
} catch (Psr6CacheException $e) {
222+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
223+
}
224+
}
225+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
15+
use Symfony\Component\Cache\Adapter\SimpleCacheAdapter;
16+
use Symfony\Component\Cache\Simple\Psr6Cache;
17+
18+
/**
19+
* @group time-sensitive
20+
*/
21+
class SimpleCacheAdapterTest extends AdapterTestCase
22+
{
23+
public function createCachePool($defaultLifetime = 0)
24+
{
25+
return new SimpleCacheAdapter(new Psr6Cache(new FilesystemAdapter()), '', $defaultLifetime);
26+
}
27+
}

0 commit comments

Comments
 (0)