[Cache] leverage Contracts\Cache

This commit is contained in:
Nicolas Grekas 2018-08-01 10:02:13 +02:00
parent 09786044c4
commit 0a1220fd96
18 changed files with 43 additions and 89 deletions

View File

@ -114,7 +114,10 @@
"psr/cache-implementation": "1.0",
"psr/container-implementation": "1.0",
"psr/log-implementation": "1.0",
"psr/simple-cache-implementation": "1.0"
"psr/simple-cache-implementation": "1.0",
"symfony/cache-contracts": "1.0",
"symfony/service-contracts": "1.0",
"symfony/translation-contracts": "1.0"
},
"autoload": {
"psr-4": {

View File

@ -149,6 +149,7 @@
<service id="Psr\Cache\CacheItemPoolInterface" alias="cache.app" />
<service id="Psr\SimpleCache\CacheInterface" alias="cache.app.simple" />
<service id="Symfony\Component\Cache\Adapter\AdapterInterface" alias="cache.app" />
<service id="Symfony\Component\Cache\CacheInterface" alias="cache.app.taggable" />
<service id="Symfony\Contracts\Cache\CacheInterface" alias="cache.app" />
<service id="Symfony\Contracts\Cache\TagAwareCacheInterface" alias="cache.app.taggable" />
</services>
</container>

View File

@ -15,12 +15,12 @@ use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\AbstractTrait;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Contracts\Cache\CacheInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>

View File

@ -13,10 +13,10 @@ namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerAwareInterface;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ArrayTrait;
use Symfony\Contracts\Cache\CacheInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>

View File

@ -13,12 +13,12 @@ namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Service\ResetInterface;
/**

View File

@ -12,8 +12,8 @@
namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Contracts\Cache\CacheInterface;
/**
* @author Titouan Galopin <galopintitouan@gmail.com>

View File

@ -13,13 +13,13 @@ namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Component\Cache\Traits\PhpArrayTrait;
use Symfony\Contracts\Cache\CacheInterface;
/**
* Caches items at warm up time using a PHP array that is stored in shared memory by OPCache since PHP 7.0.

View File

@ -13,12 +13,12 @@ namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Component\Cache\Traits\ProxyTrait;
use Symfony\Contracts\Cache\CacheInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>

View File

@ -13,17 +13,17 @@ namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Component\Cache\Traits\ProxyTrait;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class TagAwareAdapter implements CacheInterface, TagAwareAdapterInterface, PruneableInterface, ResettableInterface
class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
{
const TAGS_PREFIX = "\0tags\0";

View File

@ -12,10 +12,10 @@
namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Service\ResetInterface;
/**

View File

@ -11,12 +11,12 @@
namespace Symfony\Component\Cache\Adapter;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
/**
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class TraceableTagAwareAdapter extends TraceableAdapter implements CacheInterface, TagAwareAdapterInterface
class TraceableTagAwareAdapter extends TraceableAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface
{
public function __construct(TagAwareAdapterInterface $pool)
{

View File

@ -1,37 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache;
/**
* Gets and stores items from a cache.
*
* On cache misses, a callback is called that should return the missing value.
* It is given two arguments:
* - the missing cache key
* - the corresponding PSR-6 CacheItemInterface object,
* allowing time-based expiration control.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
interface CacheInterface
{
/**
* @param callable(CacheItem):mixed $callback Should return the computed value for the given key/item
* @param float|null $beta A float that controls the likeliness of triggering early expiration.
* 0 disables it, INF forces immediate expiration.
* The default (or providing null) is implementation dependent but should
* typically be 1.0, which should provide optimal stampede protection.
*
* @return mixed The value corresponding to the provided key
*/
public function get(string $key, callable $callback, float $beta = null);
}

View File

@ -11,31 +11,16 @@
namespace Symfony\Component\Cache;
use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\Exception\LogicException;
use Symfony\Contracts\Cache\ItemInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
final class CacheItem implements CacheItemInterface
final class CacheItem implements ItemInterface
{
/**
* References the Unix timestamp stating when the item will expire.
*/
const METADATA_EXPIRY = 'expiry';
/**
* References the time the item took to be created, in milliseconds.
*/
const METADATA_CTIME = 'ctime';
/**
* References the list of tags that were assigned to the item, as string[].
*/
const METADATA_TAGS = 'tags';
private const METADATA_EXPIRY_OFFSET = 1527506807;
protected $key;
@ -118,15 +103,9 @@ final class CacheItem implements CacheItemInterface
}
/**
* Adds a tag to a cache item.
*
* @param string|string[] $tags A tag or array of tags
*
* @return static
*
* @throws InvalidArgumentException When $tag is not valid
* {@inheritdoc}
*/
public function tag($tags)
public function tag($tags): ItemInterface
{
if (!$this->isTaggable) {
throw new LogicException(sprintf('Cache item "%s" comes from a non tag-aware pool: you cannot tag it.', $this->key));
@ -153,6 +132,14 @@ final class CacheItem implements CacheItemInterface
return $this;
}
/**
* {@inheritdoc}
*/
public function getMetadata(): array
{
return $this->metadata;
}
/**
* Returns the list of tags bound to the value coming from the pool storage if any.
*
@ -167,16 +154,6 @@ final class CacheItem implements CacheItemInterface
return $this->metadata[self::METADATA_TAGS] ?? array();
}
/**
* Returns a list of metadata info that were saved alongside with the cached value.
*
* See public CacheItem::METADATA_* consts for keys potentially found in the returned array.
*/
public function getMetadata(): array
{
return $this->metadata;
}
/**
* Validates a cache key according to PSR-6.
*

View File

@ -11,8 +11,8 @@
namespace Symfony\Component\Cache\Exception;
use Psr\Cache\InvalidArgumentException as Psr6CacheInterface;
use Psr\SimpleCache\InvalidArgumentException as SimpleCacheInterface;
use Psr\Cache\CacheException as Psr6CacheInterface;
use Psr\SimpleCache\CacheException as SimpleCacheInterface;
class LogicException extends \LogicException implements Psr6CacheInterface, SimpleCacheInterface
{

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Cache\Traits;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\LockRegistry;
/**
@ -31,6 +32,10 @@ trait GetTrait
*/
public function get(string $key, callable $callback, float $beta = null)
{
if (0 > $beta) {
throw new InvalidArgumentException(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.', \get_class($this), $beta));
}
return $this->doGet($this, $key, $callback, $beta ?? 1.0);
}

View File

@ -17,7 +17,8 @@
],
"provide": {
"psr/cache-implementation": "1.0",
"psr/simple-cache-implementation": "1.0"
"psr/simple-cache-implementation": "1.0",
"symfony/cache-contracts": "1.0"
},
"require": {
"php": "^7.1.3",

View File

@ -39,7 +39,8 @@
"symfony/yaml": "<3.4"
},
"provide": {
"psr/container-implementation": "1.0"
"psr/container-implementation": "1.0",
"symfony/service-contracts": "1.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\DependencyInjection\\": "" },

View File

@ -34,6 +34,9 @@
"symfony/dependency-injection": "<3.4",
"symfony/yaml": "<3.4"
},
"provide": {
"symfony/translation-contracts": "1.0"
},
"suggest": {
"symfony/config": "",
"symfony/yaml": "",