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 0610dd8

Browse filesBrowse files
[Cache] Create PSR-16 variants of all PSR-6 adapters
1 parent 813ea07 commit 0610dd8
Copy full SHA for 0610dd8

37 files changed

+1783
-3
lines changed

‎phpunit.xml.dist

Copy file name to clipboardExpand all lines: phpunit.xml.dist
+2Lines 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\Tests\Adapter</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>
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 Symfony\Component\Cache\Traits\ApcuTrait;
15+
16+
class ApcuAdapter extends AbstractAdapter
17+
{
18+
use ApcuTrait;
19+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 Symfony\Component\Cache\Traits\DoctrineTrait;
15+
16+
class DoctrineAdapter extends AbstractAdapter
17+
{
18+
use DoctrineTrait;
19+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 Symfony\Component\Cache\Traits\FilesystemTrait;
15+
16+
class FilesystemAdapter extends AbstractAdapter
17+
{
18+
use FilesystemTrait;
19+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 Symfony\Component\Cache\Traits\MemcachedTrait;
15+
16+
class MemcachedAdapter extends AbstractAdapter
17+
{
18+
use MemcachedTrait;
19+
20+
protected $maxIdLength = 250;
21+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 Symfony\Component\Cache\Traits\PdoTrait;
15+
16+
class PdoAdapter extends AbstractAdapter
17+
{
18+
use PdoTrait;
19+
20+
protected $maxIdLength = 255;
21+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 Symfony\Component\Cache\Traits\PhpFilesTrait;
15+
16+
class PhpFilesAdapter extends AbstractAdapter
17+
{
18+
use PhpFilesTrait;
19+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 Symfony\Component\Cache\Traits\RedisTrait;
15+
16+
class RedisAdapter extends AbstractAdapter
17+
{
18+
use RedisTrait;
19+
}
+174Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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\Log\LoggerAwareInterface;
15+
use Psr\SimpleCache\CacheInterface;
16+
use Symfony\Component\Cache\CacheItem;
17+
use Symfony\Component\Cache\Exception\InvalidArgumentException;
18+
use Symfony\Component\Cache\Traits\AbstractTrait;
19+
20+
/**
21+
* @author Nicolas Grekas <p@tchwork.com>
22+
*/
23+
abstract class AbstractCache implements CacheInterface, LoggerAwareInterface
24+
{
25+
use AbstractTrait {
26+
deleteItems as private;
27+
AbstractTrait::deleteItem as delete;
28+
AbstractTrait::hasItem as has;
29+
}
30+
31+
private $defaultLifetime;
32+
33+
protected function __construct($namespace = '', $defaultLifetime = 0)
34+
{
35+
$this->defaultLifetime = max(0, (int) $this->defaultLifetime);
36+
$this->namespace = '' === $namespace ? '' : $this->getId($namespace).':';
37+
if (null !== $this->maxIdLength && strlen($namespace) > $this->maxIdLength - 24) {
38+
throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s")', $this->maxIdLength - 24, strlen($namespace), $namespace));
39+
}
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function get($key, $default = null)
46+
{
47+
$id = $this->getId($key);
48+
49+
try {
50+
foreach ($this->doFetch(array($id)) as $value) {
51+
return $value;
52+
}
53+
} catch (\Exception $e) {
54+
CacheItem::log($this->logger, 'Failed to fetch key "{key}"', array('key' => $key, 'exception' => $e));
55+
}
56+
57+
return $default;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function set($key, $value, $ttl = null)
64+
{
65+
CacheItem::validateKey($key);
66+
67+
return $this->setMultiple(array($key => $value), $ttl);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function getMultiple($keys, $default = null)
74+
{
75+
if ($keys instanceof \Traversable) {
76+
$keys = iterator_to_array($keys, false);
77+
} elseif (!is_array($keys)) {
78+
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
79+
}
80+
$ids = array();
81+
82+
foreach ($keys as $key) {
83+
$ids[] = $this->getId($key);
84+
}
85+
try {
86+
$values = $this->doFetch($ids);
87+
} catch (\Exception $e) {
88+
CacheItem::log($this->logger, 'Failed to fetch requested values', array('keys' => $keys, 'exception' => $e));
89+
$values = array();
90+
}
91+
$ids = array_combine($ids, $keys);
92+
93+
return $this->generateValues($values, $ids, $default);
94+
}
95+
96+
/**
97+
* {@inheritdoc}
98+
*/
99+
public function setMultiple($values, $ttl = null)
100+
{
101+
if (!is_array($values) && !$values instanceof \Traversable) {
102+
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values)));
103+
}
104+
$valuesById = array();
105+
106+
foreach ($values as $key => $value) {
107+
$valuesById[$this->getId($key)] = $value;
108+
}
109+
if (false === $ttl = $this->normalizeTtl($ttl)) {
110+
return true;
111+
}
112+
113+
try {
114+
$e = $this->doSave($valuesById, $ttl);
115+
} catch (\Exception $e) {
116+
}
117+
if (true === $e || array() === $e) {
118+
return true;
119+
}
120+
$keys = array();
121+
foreach (is_array($e) ? $e : array_keys($valuesById) as $id) {
122+
$keys[] = substr($id, strlen($this->namespace));
123+
}
124+
CacheItem::log($this->logger, 'Failed to save values', array('keys' => $keys, 'exception' => $e instanceof \Exception ? $e : null));
125+
126+
return false;
127+
}
128+
129+
/**
130+
* {@inheritdoc}
131+
*/
132+
public function deleteMultiple($keys)
133+
{
134+
if ($keys instanceof \Traversable) {
135+
$keys = iterator_to_array($keys, false);
136+
} elseif (!is_array($keys)) {
137+
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
138+
}
139+
140+
return $this->deleteItems($keys);
141+
}
142+
143+
private function normalizeTtl($ttl)
144+
{
145+
if (null === $ttl) {
146+
return $this->defaultLifetime;
147+
}
148+
if ($ttl instanceof \DateInterval) {
149+
$ttl = \DateTime::createFromFormat('U', 0)->add($ttl)->format('U');
150+
}
151+
if (is_int($ttl)) {
152+
return 0 < $ttl ? $ttl : false;
153+
}
154+
155+
throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', is_object($ttl) ? get_class($ttl) : gettype($ttl)));
156+
}
157+
158+
private function generateValues($values, &$keys, $default)
159+
{
160+
try {
161+
foreach ($values as $id => $value) {
162+
$key = $keys[$id];
163+
unset($keys[$id]);
164+
yield $key => $value;
165+
}
166+
} catch (\Exception $e) {
167+
CacheItem::log($this->logger, 'Failed to fetch requested values', array('keys' => array_values($keys), 'exception' => $e));
168+
}
169+
170+
foreach ($keys as $key) {
171+
yield $key => $default;
172+
}
173+
}
174+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 Symfony\Component\Cache\Traits\ApcuTrait;
15+
16+
class ApcuCache extends AbstractCache
17+
{
18+
use ApcuTrait;
19+
}

0 commit comments

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