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 1ae92f6

Browse filesBrowse files
[Cache] Create PSR-16 variants of all PSR-6 adapters
1 parent cb453cd commit 1ae92f6
Copy full SHA for 1ae92f6

Some content is hidden

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

48 files changed

+2314
-67
lines changed
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
20+
public function __construct($namespace = '', $defaultLifetime = 0, $version = null)
21+
{
22+
$this->init($namespace, $defaultLifetime, $version);
23+
}
24+
}
+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\Adapter;
13+
14+
use Doctrine\Common\Cache\CacheProvider;
15+
use Symfony\Component\Cache\Traits\DoctrineTrait;
16+
17+
class DoctrineAdapter extends AbstractAdapter
18+
{
19+
use DoctrineTrait;
20+
21+
public function __construct(CacheProvider $provider, $namespace = '', $defaultLifetime = 0)
22+
{
23+
parent::__construct('', $defaultLifetime);
24+
$this->provider = $provider;
25+
$provider->setNamespace($namespace);
26+
}
27+
}
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
20+
public function __construct($namespace = '', $defaultLifetime = 0, $directory = null)
21+
{
22+
parent::__construct('', $defaultLifetime);
23+
$this->init($namespace, $directory);
24+
}
25+
}
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
22+
public function __construct(\Memcached $client, $namespace = '', $defaultLifetime = 0)
23+
{
24+
$this->init($client, $namespace, $defaultLifetime);
25+
}
26+
}
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
22+
/**
23+
* Constructor.
24+
*
25+
* You can either pass an existing database connection as PDO instance or
26+
* a Doctrine DBAL Connection or a DSN string that will be used to
27+
* lazy-connect to the database when the cache is actually used.
28+
*
29+
* List of available options:
30+
* * db_table: The name of the table [default: cache_items]
31+
* * db_id_col: The column where to store the cache id [default: item_id]
32+
* * db_data_col: The column where to store the cache data [default: item_data]
33+
* * db_lifetime_col: The column where to store the lifetime [default: item_lifetime]
34+
* * db_time_col: The column where to store the timestamp [default: item_time]
35+
* * db_username: The username when lazy-connect [default: '']
36+
* * db_password: The password when lazy-connect [default: '']
37+
* * db_connection_options: An array of driver-specific connection options [default: array()]
38+
*
39+
* @param \PDO|Connection|string $connOrDsn A \PDO or Connection instance or DSN string or null
40+
* @param string $namespace
41+
* @param int $defaultLifetime
42+
* @param array $options An associative array of options
43+
*
44+
* @throws InvalidArgumentException When first argument is not PDO nor Connection nor string
45+
* @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
46+
* @throws InvalidArgumentException When namespace contains invalid characters
47+
*/
48+
public function __construct($connOrDsn, $namespace = '', $defaultLifetime = 0, array $options = array())
49+
{
50+
$this->init($connOrDsn, $namespace, $defaultLifetime, $options);
51+
}
52+
}
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
20+
public function __construct($namespace = '', $defaultLifetime = 0, $directory = null)
21+
{
22+
if (!static::isSupported()) {
23+
throw new CacheException('OPcache is not enabled');
24+
}
25+
parent::__construct('', $defaultLifetime);
26+
$this->init($namespace, $directory);
27+
28+
$e = new \Exception();
29+
$this->includeHandler = function () use ($e) { throw $e; };
30+
}
31+
}
+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\Adapter;
13+
14+
use Symfony\Component\Cache\Traits\RedisTrait;
15+
16+
class RedisAdapter extends AbstractAdapter
17+
{
18+
use RedisTrait;
19+
20+
/**
21+
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient
22+
*/
23+
public function __construct($redisClient, $namespace = '', $defaultLifetime = 0)
24+
{
25+
$this->init($redisClient, $namespace, $defaultLifetime);
26+
}
27+
}
+177Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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) $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+
if (is_int($key)) {
108+
$key = (string) $key;
109+
}
110+
$valuesById[$this->getId($key)] = $value;
111+
}
112+
if (false === $ttl = $this->normalizeTtl($ttl)) {
113+
return $this->doDelete(array_keys($valuesById));
114+
}
115+
116+
try {
117+
$e = $this->doSave($valuesById, $ttl);
118+
} catch (\Exception $e) {
119+
}
120+
if (true === $e || array() === $e) {
121+
return true;
122+
}
123+
$keys = array();
124+
foreach (is_array($e) ? $e : array_keys($valuesById) as $id) {
125+
$keys[] = substr($id, strlen($this->namespace));
126+
}
127+
CacheItem::log($this->logger, 'Failed to save values', array('keys' => $keys, 'exception' => $e instanceof \Exception ? $e : null));
128+
129+
return false;
130+
}
131+
132+
/**
133+
* {@inheritdoc}
134+
*/
135+
public function deleteMultiple($keys)
136+
{
137+
if ($keys instanceof \Traversable) {
138+
$keys = iterator_to_array($keys, false);
139+
} elseif (!is_array($keys)) {
140+
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
141+
}
142+
143+
return $this->deleteItems($keys);
144+
}
145+
146+
private function normalizeTtl($ttl)
147+
{
148+
if (null === $ttl) {
149+
return $this->defaultLifetime;
150+
}
151+
if ($ttl instanceof \DateInterval) {
152+
$ttl = (int) \DateTime::createFromFormat('U', 0)->add($ttl)->format('U');
153+
}
154+
if (is_int($ttl)) {
155+
return 0 < $ttl ? $ttl : false;
156+
}
157+
158+
throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', is_object($ttl) ? get_class($ttl) : gettype($ttl)));
159+
}
160+
161+
private function generateValues($values, &$keys, $default)
162+
{
163+
try {
164+
foreach ($values as $id => $value) {
165+
$key = $keys[$id];
166+
unset($keys[$id]);
167+
yield $key => $value;
168+
}
169+
} catch (\Exception $e) {
170+
CacheItem::log($this->logger, 'Failed to fetch requested values', array('keys' => array_values($keys), 'exception' => $e));
171+
}
172+
173+
foreach ($keys as $key) {
174+
yield $key => $default;
175+
}
176+
}
177+
}

0 commit comments

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