Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da68dae

Browse filesBrowse files
[Cache] Implement PSR-16 SimpleCache v0.3
1 parent 13265ae commit da68dae
Copy full SHA for da68dae

File tree

7 files changed

+332
-8
lines changed
Filter options

7 files changed

+332
-8
lines changed

‎composer.json

Copy file name to clipboardExpand all lines: composer.json
+3-1Lines changed: 3 additions & 1 deletion
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": "^0.3",
2425
"symfony/polyfill-intl-icu": "~1.0",
2526
"symfony/polyfill-mbstring": "~1.0",
2627
"symfony/polyfill-php56": "~1.0",
@@ -97,7 +98,8 @@
9798
"phpdocumentor/type-resolver": "<0.2.0"
9899
},
99100
"provide": {
100-
"psr/cache-implementation": "1.0"
101+
"psr/cache-implementation": "1.0",
102+
"psr/simple-cache-implementation": "0.3"
101103
},
102104
"autoload": {
103105
"psr-4": {
+75Lines 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 <fabien@symfony.com>
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 <p@tchwork.com>
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Exception/CacheException.php
+3-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Exception/InvalidArgumentException.php
+3-2Lines 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
}
+216Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 <p@tchwork.com>
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) {
36+
CacheItem::validateKey($key);
37+
$item = new CacheItem();
38+
$item->key = $key;
39+
$item->value = $value;
40+
41+
return $item;
42+
},
43+
null,
44+
CacheItem::class
45+
);
46+
}
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function get($key, $default = null)
53+
{
54+
try {
55+
$item = $this->pool->getItem($key);
56+
} catch (SimpleCacheException $e) {
57+
throw $e;
58+
} catch (Psr6CacheException $e) {
59+
throw new InvalidArgumentException($e->getMessage(), 0, $e);
60+
}
61+
62+
return $item->isHit() ? $item->get() : $default;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function set($key, $value, $ttl = null)
69+
{
70+
try {
71+
if (null !== $f = $this->createCacheItem) {
72+
$item = $f($key, $value);
73+
} else {
74+
$item = $this->pool->getItem($key)->set($value);
75+
}
76+
} catch (SimpleCacheException $e) {
77+
throw $e;
78+
} catch (Psr6CacheException $e) {
79+
throw new InvalidArgumentException($e->getMessage(), 0, $e);
80+
}
81+
if (null !== $ttl) {
82+
$item->expiresAfter($ttl);
83+
}
84+
85+
return $this->pool->save($item);
86+
}
87+
88+
/**
89+
* {@inheritdoc}
90+
*/
91+
public function delete($key)
92+
{
93+
try {
94+
return $this->pool->deleteItem($key);
95+
} catch (SimpleCacheException $e) {
96+
throw $e;
97+
} catch (Psr6CacheException $e) {
98+
throw new InvalidArgumentException($e->getMessage(), 0, $e);
99+
}
100+
}
101+
102+
/**
103+
* {@inheritdoc}
104+
*/
105+
public function clear()
106+
{
107+
return $this->pool->clear();
108+
}
109+
110+
/**
111+
* {@inheritdoc}
112+
*/
113+
public function getMultiple($keys, $default = null)
114+
{
115+
if ($keys instanceof \Traversable) {
116+
$keys = iterator_to_array($keys, false);
117+
} elseif (!is_array($keys)) {
118+
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
119+
}
120+
121+
try {
122+
$items = $this->pool->getItems($keys);
123+
} catch (SimpleCacheException $e) {
124+
throw $e;
125+
} catch (Psr6CacheException $e) {
126+
throw new InvalidArgumentException($e->getMessage(), 0, $e);
127+
}
128+
$values = array();
129+
130+
foreach ($items as $key => $item) {
131+
$values[$key] = $item->isHit() ? $item->get() : $default;
132+
}
133+
134+
return $values;
135+
}
136+
137+
/**
138+
* {@inheritdoc}
139+
*/
140+
public function setMultiple($values, $ttl = null)
141+
{
142+
$valuesIsArray = is_array($values);
143+
if (!$valuesIsArray && !$values instanceof \Traversable) {
144+
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values)));
145+
}
146+
$items = array();
147+
148+
try {
149+
if (null !== $f = $this->createCacheItem) {
150+
$valuesIsArray = false;
151+
foreach ($values as $key => $value) {
152+
$items[$key] = $f($key, $value);
153+
}
154+
} elseif ($valuesIsArray) {
155+
$items = $this->pool->getItems(array_keys($values));
156+
} else {
157+
foreach ($values as $key => $value) {
158+
if (!isset($items[$key])) {
159+
$items[$key] = $this->pool->getItem($key)->set($value);
160+
}
161+
}
162+
}
163+
} catch (SimpleCacheException $e) {
164+
throw $e;
165+
} catch (Psr6CacheException $e) {
166+
throw new InvalidArgumentException($e->getMessage(), 0, $e);
167+
}
168+
$ok = true;
169+
170+
foreach ($items as $key => $item) {
171+
if ($valuesIsArray) {
172+
$item->set($values[$key]);
173+
}
174+
if (null !== $ttl) {
175+
$item->expiresAfter($ttl);
176+
}
177+
$ok = $this->pool->saveDeferred($item) && $ok;
178+
}
179+
180+
return $this->pool->commit() && $ok;
181+
}
182+
183+
/**
184+
* {@inheritdoc}
185+
*/
186+
public function deleteMultiple($keys)
187+
{
188+
if ($keys instanceof \Traversable) {
189+
$keys = iterator_to_array($keys, false);
190+
} elseif (!is_array($keys)) {
191+
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
192+
}
193+
194+
try {
195+
return $this->pool->deleteItems($keys);
196+
} catch (SimpleCacheException $e) {
197+
throw $e;
198+
} catch (Psr6CacheException $e) {
199+
throw new InvalidArgumentException($e->getMessage(), 0, $e);
200+
}
201+
}
202+
203+
/**
204+
* {@inheritdoc}
205+
*/
206+
public function has($key)
207+
{
208+
try {
209+
return $this->pool->hasItem($key);
210+
} catch (SimpleCacheException $e) {
211+
throw $e;
212+
} catch (Psr6CacheException $e) {
213+
throw new InvalidArgumentException($e->getMessage(), 0, $e);
214+
}
215+
}
216+
}
+27Lines 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 <fabien@symfony.com>
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+
}

‎src/Symfony/Component/Cache/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/composer.json
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "symfony/cache",
33
"type": "library",
4-
"description": "Symfony implementation of PSR-6",
4+
"description": "Symfony Cache component with PSR-6, PSR-16, and tags",
55
"keywords": ["caching", "psr6"],
66
"homepage": "https://symfony.com",
77
"license": "MIT",
@@ -16,12 +16,14 @@
1616
}
1717
],
1818
"provide": {
19-
"psr/cache-implementation": "1.0"
19+
"psr/cache-implementation": "1.0",
20+
"psr/simple-cache-implementation": "0.3"
2021
},
2122
"require": {
2223
"php": ">=5.5.9",
2324
"psr/cache": "~1.0",
24-
"psr/log": "~1.0"
25+
"psr/log": "~1.0",
26+
"psr/simple-cache": "^0.3"
2527
},
2628
"require-dev": {
2729
"cache/integration-tests": "dev-master",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.