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

[Cache] Add argument $prefix to AdapterInterface::clear() #32284

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 8, 2019
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
5 changes: 5 additions & 0 deletions 5 UPGRADE-4.4.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
UPGRADE FROM 4.3 to 4.4
=======================

Cache
-----

* Added argument `$prefix` to `AdapterInterface::clear()`

DependencyInjection
-------------------

Expand Down
1 change: 1 addition & 0 deletions 1 UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Cache
* Removed `CacheItem::getPreviousTags()`, use `CacheItem::getMetadata()` instead.
* Removed all PSR-16 adapters, use `Psr16Cache` or `Symfony\Contracts\Cache\CacheInterface` implementations instead.
* Removed `SimpleCacheAdapter`, use `Psr16Adapter` instead.
* Added argument `$prefix` to `AdapterInterface::clear()`

Config
------
Expand Down
7 changes: 7 additions & 0 deletions 7 src/Symfony/Component/Cache/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public function getItem($key);
* @return \Traversable|CacheItem[]
*/
public function getItems(array $keys = []);

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear(/*string $prefix = ''*/);
}
11 changes: 9 additions & 2 deletions 11 src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,21 @@ public function hasItem($key)

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear()
public function clear(/*string $prefix = ''*/)
{
$prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';
$cleared = true;
$i = $this->adapterCount;

while ($i--) {
$cleared = $this->adapters[$i]->clear() && $cleared;
if ($this->adapters[$i] instanceof AdapterInterface) {
$cleared = $this->adapters[$i]->clear($prefix) && $cleared;
} else {
$cleared = $this->adapters[$i]->clear() && $cleared;
}
}

return $cleared;
Expand Down
4 changes: 3 additions & 1 deletion 4 src/Symfony/Component/Cache/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ public function hasItem($key)

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear()
public function clear(/*string $prefix = ''*/)
{
return true;
}
Expand Down
10 changes: 9 additions & 1 deletion 10 src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,17 @@ public function hasItem($key)

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear()
public function clear(/*string $prefix = ''*/)
{
$prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';

if ($this->pool instanceof AdapterInterface) {
return $this->pool->clear($this->namespace.$prefix);
Copy link
Member Author

Choose a reason for hiding this comment

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

here is the targetted behavior of this new argument

}

return $this->pool->clear();
}

Expand Down
20 changes: 18 additions & 2 deletions 20 src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,26 @@ public function getItems(array $keys = [])

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear()
public function clear(/*string $prefix = ''*/)
{
$this->deferred = [];
$prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';

if ('' !== $prefix) {
foreach ($this->deferred as $key => $item) {
if (0 === strpos($key, $prefix)) {
unset($this->deferred[$key]);
}
}
} else {
$this->deferred = [];
}

if ($this->pool instanceof AdapterInterface) {
return $this->pool->clear($prefix);
}

return $this->pool->clear();
}
Expand Down
9 changes: 8 additions & 1 deletion 9 src/Symfony/Component/Cache/Adapter/TraceableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,18 @@ public function getItems(array $keys = [])

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear()
public function clear(/*string $prefix = ''*/)
{
$prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';
$event = $this->start(__FUNCTION__);
try {
if ($this->pool instanceof AdapterInterface) {
return $event->result = $this->pool->clear($prefix);
}

return $event->result = $this->pool->clear();
} finally {
$event->end = microtime(true);
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added support for connecting to Redis Sentinel clusters
* added argument `$prefix` to `AdapterInterface::clear()`

4.3.0
-----
Expand Down
20 changes: 20 additions & 0 deletions 20 src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ public function testPrune()
$this->assertFalse($this->isPruned($cache, 'foo'));
$this->assertTrue($this->isPruned($cache, 'qux'));
}

public function testClearPrefix()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$cache = $this->createCachePool(0, __FUNCTION__);
$cache->clear();

$item = $cache->getItem('foobar');
$cache->save($item->set(1));

$item = $cache->getItem('barfoo');
$cache->save($item->set(2));

$cache->clear('foo');
$this->assertFalse($cache->hasItem('foobar'));
$this->assertTrue($cache->hasItem('barfoo'));
}
}

class NotUnserializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DoctrineAdapterTest extends AdapterTestCase
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayCache is not.',
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayCache is not.',
'testNotUnserializable' => 'ArrayCache does not use serialize/unserialize',
'testClearPrefix' => 'Doctrine cannot clear by prefix',
];

public function createCachePool($defaultLifetime = 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MemcachedAdapterTest extends AdapterTestCase
protected $skippedTests = [
'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
'testClearPrefix' => 'Memcached cannot clear by prefix',
];

protected static $client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function tearDown()

public function createCachePool($defaultLifetime = 0, $testMethod = null)
{
if ('testGetMetadata' === $testMethod) {
if ('testGetMetadata' === $testMethod || 'testClearPrefix' === $testMethod) {
return new PhpArrayAdapter(self::$file, new FilesystemAdapter());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Psr16AdapterTest extends AdapterTestCase
{
protected $skippedTests = [
'testPrune' => 'Psr16adapter just proxies',
'testClearPrefix' => 'SimpleCache cannot clear by prefix',
];

public function createCachePool($defaultLifetime = 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SimpleCacheAdapterTest extends AdapterTestCase
{
protected $skippedTests = [
'testPrune' => 'SimpleCache just proxies',
'testClearPrefix' => 'SimpleCache cannot clear by prefix',
];

public function createCachePool($defaultLifetime = 0)
Expand Down
7 changes: 5 additions & 2 deletions 7 src/Symfony/Component/Cache/Traits/AbstractTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ public function hasItem($key)

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear()
public function clear(/*string $prefix = ''*/)
{
$prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';
$this->deferred = [];
if ($cleared = $this->versioningIsEnabled) {
$namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), static::NS_SEPARATOR, 5);
Expand All @@ -120,7 +123,7 @@ public function clear()
}

try {
return $this->doClear($this->namespace) || $cleared;
return $this->doClear($this->namespace.$prefix) || $cleared;
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to clear the cache: '.$e->getMessage(), ['exception' => $e]);

Expand Down
16 changes: 14 additions & 2 deletions 16 src/Symfony/Component/Cache/Traits/ArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,22 @@ public function hasItem($key)

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear()
public function clear(/*string $prefix = ''*/)
{
$this->values = $this->expiries = [];
$prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';

if ('' !== $prefix) {
foreach ($this->values as $key => $value) {
if (0 === strpos($key, $prefix)) {
unset($this->values[$key], $this->expiries[$key]);
}
}
} else {
$this->values = $this->expiries = [];
}

return true;
}
Expand Down
9 changes: 9 additions & 0 deletions 9 src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ protected function doClear($namespace)
$ok = true;

foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS)) as $file) {
if ('' !== $namespace && 0 !== strpos($this->getFileKey($file), $namespace)) {
continue;
}

$ok = ($file->isDir() || $this->doUnlink($file) || !file_exists($file)) && $ok;
}

Expand Down Expand Up @@ -114,6 +118,11 @@ private function getFile($id, $mkdir = false, string $directory = null)
return $dir.substr($hash, 2, 20);
}

private function getFileKey(string $file): string
{
return '';
}

/**
* @internal
*/
Expand Down
13 changes: 13 additions & 0 deletions 13 src/Symfony/Component/Cache/Traits/FilesystemTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,17 @@ protected function doSave(array $values, $lifetime)

return $failed;
}

private function getFileKey(string $file): string
{
if (!$h = @fopen($file, 'rb')) {
return '';
}

fgets($h); // expiry
$encodedKey = fgets($h);
fclose($h);

return rawurldecode(rtrim($encodedKey));
}
}
10 changes: 9 additions & 1 deletion 10 src/Symfony/Component/Cache/Traits/PhpArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Cache\Traits;

use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\VarExporter\VarExporter;
Expand Down Expand Up @@ -121,13 +122,20 @@ public function warmUp(array $values)

/**
* {@inheritdoc}
*
* @param string $prefix
*/
public function clear()
public function clear(/*string $prefix = ''*/)
{
$prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';
$this->keys = $this->values = [];

$cleared = @unlink($this->file) || !file_exists($this->file);

if ($this->pool instanceof AdapterInterface) {
return $this->pool->clear($prefix) && $cleared;
}

return $this->pool->clear() && $cleared;
}

Expand Down
20 changes: 17 additions & 3 deletions 20 src/Symfony/Component/Cache/Traits/PhpFilesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,19 @@ protected function doSave(array $values, $lifetime)
$value = var_export($value, true);
}

$encodedKey = rawurlencode($key);

if (!$isStaticValue) {
// We cannot use a closure here because of https://bugs.php.net/76982
$value = str_replace('\Symfony\Component\VarExporter\Internal\\', '', $value);
$value = "<?php\n\nnamespace Symfony\Component\VarExporter\Internal;\n\nreturn \$getExpiry ? {$expiry} : {$value};\n";
$value = "namespace Symfony\Component\VarExporter\Internal;\n\nreturn \$getExpiry ? {$expiry} : {$value};";
} else {
$value = "<?php return [{$expiry}, {$value}];\n";
$value = "return [{$expiry}, {$value}];";
}

$file = $this->files[$key] = $this->getFile($key, true);
// Since OPcache only compiles files older than the script execution start, set the file's mtime in the past
$ok = $this->write($file, $value, self::$startTime - 10) && $ok;
$ok = $this->write($file, "<?php //{$encodedKey}\n\n{$value}\n", self::$startTime - 10) && $ok;

if ($allowCompile) {
@opcache_invalidate($file, true);
Expand Down Expand Up @@ -266,6 +268,18 @@ protected function doUnlink($file)

return @unlink($file);
}

private function getFileKey(string $file): string
{
if (!$h = @fopen($file, 'rb')) {
return '';
}

$encodedKey = substr(fgets($h), 8);
fclose($h);

return rawurldecode(rtrim($encodedKey));
}
}

/**
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.