-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Cache] Use namespace versioning for backends that dont support clearing by keys #23969
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ trait AbstractTrait | |
use LoggerAwareTrait; | ||
|
||
private $namespace; | ||
private $namespaceVersion = ''; | ||
private $versioningIsEnabled = false; | ||
private $deferred = array(); | ||
|
||
/** | ||
|
@@ -102,10 +104,18 @@ public function hasItem($key) | |
*/ | ||
public function clear() | ||
{ | ||
if ($cleared = $this->versioningIsEnabled) { | ||
$this->namespaceVersion = 2; | ||
foreach ($this->doFetch(array('@'.$this->namespace)) as $v) { | ||
$this->namespaceVersion = 1 + (int) $v; | ||
} | ||
$this->namespaceVersion .= ':'; | ||
$cleared = $this->doSave(array('@'.$this->namespace => $this->namespaceVersion), 0); | ||
} | ||
$this->deferred = array(); | ||
|
||
try { | ||
return $this->doClear($this->namespace); | ||
return $this->doClear($this->namespace) || $cleared; | ||
} catch (\Exception $e) { | ||
CacheItem::log($this->logger, 'Failed to clear the cache', array('exception' => $e)); | ||
|
||
|
@@ -158,6 +168,27 @@ public function deleteItems(array $keys) | |
return $ok; | ||
} | ||
|
||
/** | ||
* Enables/disables versioning of items. | ||
* | ||
* When versioning is enabled, clearing the cache is atomic and doesn't require listing existing keys to proceed, | ||
* but old keys may need garbage collection and extra round-trips to the back-end are required. | ||
* | ||
* Calling this method also clears the memoized namespace version and thus forces a resynchonization of it. | ||
* | ||
* @param bool $enable | ||
* | ||
* @return bool the previous state of versioning | ||
*/ | ||
public function enableVersioning($enable = true) | ||
{ | ||
$wasEnabled = $this->versioningIsEnabled; | ||
$this->versioningIsEnabled = (bool) $enable; | ||
$this->namespaceVersion = ''; | ||
|
||
return $wasEnabled; | ||
} | ||
|
||
/** | ||
* Like the native unserialize() function but throws an exception if anything goes wrong. | ||
* | ||
|
@@ -189,11 +220,18 @@ private function getId($key) | |
{ | ||
CacheItem::validateKey($key); | ||
|
||
if ($this->versioningIsEnabled && '' === $this->namespaceVersion) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other classes used the trick There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure it applies here |
||
$this->namespaceVersion = '1:'; | ||
foreach ($this->doFetch(array('@'.$this->namespace)) as $v) { | ||
$this->namespaceVersion = $v; | ||
} | ||
} | ||
|
||
if (null === $this->maxIdLength) { | ||
return $this->namespace.$key; | ||
return $this->namespace.$this->namespaceVersion.$key; | ||
} | ||
if (strlen($id = $this->namespace.$key) > $this->maxIdLength) { | ||
$id = $this->namespace.substr_replace(base64_encode(hash('sha256', $key, true)), ':', -22); | ||
if (strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) { | ||
$id = $this->namespace.$this->namespaceVersion.substr_replace(base64_encode(hash('sha256', $key, true)), ':', -22); | ||
} | ||
|
||
return $id; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolas-grekas Spelling errors: "memoized" -> "memorized" and "resynchonization" -> "resynchronization"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reported the "memoized" typo once ... and I was wrong :( "Memoize" seems to be a valid concept: https://en.wikipedia.org/wiki/Memoization Maybe it's used correctly this time too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah; seems you are correct! I think the latter one is a spelling error though. ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote memoize on purpose :)