-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[10.x] More scalable Redis cache tags #45690
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7cca797
initial work on more scalable redis cache tags
taylorotwell 6590b4c
Apply fixes from StyleCI
StyleCIBot 4b93e69
add integration test
taylorotwell 1a1521f
update tests
taylorotwell d0cc951
remove commented code
taylorotwell af5de80
use nx flag
taylorotwell 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace Illuminate\Cache; | ||
|
||
use Illuminate\Support\LazyCollection; | ||
|
||
class RedisTagSet extends TagSet | ||
{ | ||
/** | ||
* Add a reference entry to the tag set's underlying sorted set. | ||
* | ||
* @param string $key | ||
* @param int $ttl | ||
* @param string $updateWhen | ||
* @return void | ||
*/ | ||
public function addEntry(string $key, int $ttl = 0, $updateWhen = null) | ||
{ | ||
$ttl = $ttl > 0 ? now()->addSeconds($ttl)->getTimestamp() : -1; | ||
|
||
foreach ($this->tagIds() as $tagKey) { | ||
if ($updateWhen) { | ||
$this->store->connection()->zadd($this->store->getPrefix().$tagKey, $updateWhen, $ttl, $key); | ||
} else { | ||
$this->store->connection()->zadd($this->store->getPrefix().$tagKey, $ttl, $key); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get all of the cache entry keys for the tag set. | ||
* | ||
* @return \Illuminate\Support\LazyCollection | ||
*/ | ||
public function entries() | ||
{ | ||
return LazyCollection::make(function () { | ||
foreach ($this->tagIds() as $tagKey) { | ||
$cursor = $defaultCursorValue = '0'; | ||
|
||
do { | ||
[$cursor, $entries] = $this->store->connection()->zscan( | ||
$this->store->getPrefix().$tagKey, | ||
$cursor, | ||
['match' => '*', 'count' => 1000] | ||
); | ||
|
||
if (! is_array($entries)) { | ||
break; | ||
} | ||
|
||
$entries = array_unique(array_keys($entries)); | ||
|
||
if (count($entries) === 0) { | ||
continue; | ||
} | ||
|
||
foreach ($entries as $entry) { | ||
yield $entry; | ||
} | ||
} while (((string) $cursor) !== $defaultCursorValue); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Remove the stale entries from the tag set. | ||
* | ||
* @return void | ||
*/ | ||
public function flushStaleEntries() | ||
{ | ||
$this->store->connection()->pipeline(function ($pipe) { | ||
foreach ($this->tagIds() as $tagKey) { | ||
$pipe->zremrangebyscore($this->store->getPrefix().$tagKey, 0, now()->getTimestamp()); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Flush the tag from the cache. | ||
* | ||
* @param string $name | ||
*/ | ||
public function flushTag($name) | ||
{ | ||
return $this->resetTag($name); | ||
} | ||
|
||
/** | ||
* Reset the tag and return the new tag identifier. | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public function resetTag($name) | ||
{ | ||
$this->store->forget($this->tagKey($name)); | ||
|
||
return $this->tagId($name); | ||
} | ||
|
||
/** | ||
* Get the unique tag identifier for a given tag. | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public function tagId($name) | ||
{ | ||
return "tag:{$name}:entries"; | ||
} | ||
|
||
/** | ||
* Get the tag identifier key for a given tag. | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public function tagKey($name) | ||
{ | ||
return "tag:{$name}:entries"; | ||
} | ||
} |
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
Oops, something went wrong.
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.
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.
tagKey
used to return just "tag:$name:key" whose value pointed to the name of tag reference key, whiletagId
returned the reference key itself (laravel:6307213384953462982890:standard_ref
). Technically, if somebody used those for whatever reason, it will be a breaking change for them.Same goes for
resetTag
- if somebody calls that and expects the tag key to be present in cache afterwards (which is what used to happen), this is also technically a breaking change.Both of these aren't major at all and I doubt many people used these at all, but it's worth mentioning.
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.
Couldn't see a change in the tests for this kind of intermediate entries being stored properly either. Unless I missed it.