Skip to content

[Cache] Implement psr/cache 3 #41290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"provide": {
"php-http/async-client-implementation": "*",
"php-http/client-implementation": "*",
"psr/cache-implementation": "1.0|2.0",
"psr/cache-implementation": "1.0|2.0|3.0",
"psr/container-implementation": "1.0",
"psr/event-dispatcher-implementation": "1.0",
"psr/http-client-implementation": "1.0",
Expand All @@ -38,7 +38,7 @@
"doctrine/event-manager": "~1.0",
"doctrine/persistence": "^2",
"twig/twig": "^2.13|^3.0.4",
"psr/cache": "^1.0|^2.0",
"psr/cache": "^1.0|^2.0|^3.0",
"psr/container": "^1.0",
"psr/event-dispatcher": "^1.0",
"psr/link": "^1.1",
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,8 @@ public static function createConnection(string $dsn, array $options = [])

/**
* {@inheritdoc}
*
* @return bool
*/
public function commit()
public function commit(): bool
{
$ok = true;
$byLifetime = (self::$mergeByLifetime)($this->deferred, $this->namespace, $expiredIds, \Closure::fromCallable([$this, 'getId']), $this->defaultLifetime);
Expand Down
12 changes: 4 additions & 8 deletions src/Symfony/Component/Cache/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,18 @@ interface AdapterInterface extends CacheItemPoolInterface
{
/**
* {@inheritdoc}
*
* @return CacheItem
*/
public function getItem($key);
public function getItem($key): CacheItem;

/**
* {@inheritdoc}
*
* @return \Traversable|CacheItem[]
* @return iterable<CacheItem>
*/
public function getItems(array $keys = []);
public function getItems(array $keys = []): iterable;

/**
* {@inheritdoc}
*
* @return bool
*/
public function clear(string $prefix = '');
public function clear(string $prefix = ''): bool;
}
38 changes: 11 additions & 27 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ public function delete(string $key): bool

/**
* {@inheritdoc}
*
* @return bool
*/
public function hasItem($key)
public function hasItem($key): bool
{
if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) {
if ($this->maxItems) {
Expand All @@ -120,7 +118,7 @@ public function hasItem($key)
/**
* {@inheritdoc}
*/
public function getItem($key)
public function getItem($key): CacheItem
{
if (!$isHit = $this->hasItem($key)) {
$value = null;
Expand All @@ -139,7 +137,7 @@ public function getItem($key)
/**
* {@inheritdoc}
*/
public function getItems(array $keys = [])
public function getItems(array $keys = []): iterable
{
\assert(self::validateKeys($keys));

Expand All @@ -148,10 +146,8 @@ public function getItems(array $keys = [])

/**
* {@inheritdoc}
*
* @return bool
*/
public function deleteItem($key)
public function deleteItem($key): bool
{
\assert('' !== CacheItem::validateKey($key));
unset($this->values[$key], $this->expiries[$key]);
Expand All @@ -161,10 +157,8 @@ public function deleteItem($key)

/**
* {@inheritdoc}
*
* @return bool
*/
public function deleteItems(array $keys)
public function deleteItems(array $keys): bool
{
foreach ($keys as $key) {
$this->deleteItem($key);
Expand All @@ -175,10 +169,8 @@ public function deleteItems(array $keys)

/**
* {@inheritdoc}
*
* @return bool
*/
public function save(CacheItemInterface $item)
public function save(CacheItemInterface $item): bool
{
if (!$item instanceof CacheItem) {
return false;
Expand Down Expand Up @@ -230,30 +222,24 @@ public function save(CacheItemInterface $item)

/**
* {@inheritdoc}
*
* @return bool
*/
public function saveDeferred(CacheItemInterface $item)
public function saveDeferred(CacheItemInterface $item): bool
{
return $this->save($item);
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function commit()
public function commit(): bool
{
return true;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function clear(string $prefix = '')
public function clear(string $prefix = ''): bool
{
if ('' !== $prefix) {
$now = microtime(true);
Expand All @@ -276,10 +262,8 @@ public function clear(string $prefix = '')

/**
* Returns all cached values, with cache miss as null.
*
* @return array
*/
public function getValues()
public function getValues(): array
{
if (!$this->storeSerialized) {
return $this->values;
Expand All @@ -306,7 +290,7 @@ public function reset()
$this->clear();
}

private function generateItems(array $keys, $now, $f)
private function generateItems(array $keys, $now, $f): \Generator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all additions to private (and final if any) methods should be split on a lower branch

{
foreach ($keys as $i => $key) {
if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > $now || !$this->deleteItem($key))) {
Expand Down
34 changes: 10 additions & 24 deletions src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
/**
* {@inheritdoc}
*/
public function getItem($key)
public function getItem($key): CacheItem
{
$syncItem = self::$syncItem;
$misses = [];
Expand All @@ -145,12 +145,12 @@ public function getItem($key)
/**
* {@inheritdoc}
*/
public function getItems(array $keys = [])
public function getItems(array $keys = []): iterable
{
return $this->generateItems($this->adapters[0]->getItems($keys), 0);
}

private function generateItems(iterable $items, int $adapterIndex)
private function generateItems(iterable $items, int $adapterIndex): \Generator
{
$missing = [];
$misses = [];
Expand Down Expand Up @@ -183,10 +183,8 @@ private function generateItems(iterable $items, int $adapterIndex)

/**
* {@inheritdoc}
*
* @return bool
*/
public function hasItem($key)
public function hasItem($key): bool
{
foreach ($this->adapters as $adapter) {
if ($adapter->hasItem($key)) {
Expand All @@ -199,10 +197,8 @@ public function hasItem($key)

/**
* {@inheritdoc}
*
* @return bool
*/
public function clear(string $prefix = '')
public function clear(string $prefix = ''): bool
{
$cleared = true;
$i = $this->adapterCount;
Expand All @@ -220,10 +216,8 @@ public function clear(string $prefix = '')

/**
* {@inheritdoc}
*
* @return bool
*/
public function deleteItem($key)
public function deleteItem($key): bool
{
$deleted = true;
$i = $this->adapterCount;
Expand All @@ -237,10 +231,8 @@ public function deleteItem($key)

/**
* {@inheritdoc}
*
* @return bool
*/
public function deleteItems(array $keys)
public function deleteItems(array $keys): bool
{
$deleted = true;
$i = $this->adapterCount;
Expand All @@ -254,10 +246,8 @@ public function deleteItems(array $keys)

/**
* {@inheritdoc}
*
* @return bool
*/
public function save(CacheItemInterface $item)
public function save(CacheItemInterface $item): bool
{
$saved = true;
$i = $this->adapterCount;
Expand All @@ -271,10 +261,8 @@ public function save(CacheItemInterface $item)

/**
* {@inheritdoc}
*
* @return bool
*/
public function saveDeferred(CacheItemInterface $item)
public function saveDeferred(CacheItemInterface $item): bool
{
$saved = true;
$i = $this->adapterCount;
Expand All @@ -288,10 +276,8 @@ public function saveDeferred(CacheItemInterface $item)

/**
* {@inheritdoc}
*
* @return bool
*/
public function commit()
public function commit(): bool
{
$committed = true;
$i = $this->adapterCount;
Expand Down
34 changes: 10 additions & 24 deletions src/Symfony/Component/Cache/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,85 +50,71 @@ public function get(string $key, callable $callback, float $beta = null, array &
/**
* {@inheritdoc}
*/
public function getItem($key)
public function getItem($key): CacheItem
{
return (self::$createCacheItem)($key);
}

/**
* {@inheritdoc}
*/
public function getItems(array $keys = [])
public function getItems(array $keys = []): iterable
{
return $this->generateItems($keys);
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function hasItem($key)
public function hasItem($key): bool
{
return false;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function clear(string $prefix = '')
public function clear(string $prefix = ''): bool
{
return true;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function deleteItem($key)
public function deleteItem($key): bool
{
return true;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function deleteItems(array $keys)
public function deleteItems(array $keys): bool
{
return true;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function save(CacheItemInterface $item)
public function save(CacheItemInterface $item): bool
{
return true;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function saveDeferred(CacheItemInterface $item)
public function saveDeferred(CacheItemInterface $item): bool
{
return true;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function commit()
public function commit(): bool
{
return true;
}
Expand All @@ -141,7 +127,7 @@ public function delete(string $key): bool
return $this->deleteItem($key);
}

private function generateItems(array $keys)
private function generateItems(array $keys): \Generator
{
$f = self::$createCacheItem;

Expand Down
Loading