-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[12.x] Introduce memoized cache driver #55304
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
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?php | ||
|
||
namespace Illuminate\Cache; | ||
|
||
use Illuminate\Contracts\Cache\Store; | ||
|
||
class MemoizedStore implements Store | ||
{ | ||
/** | ||
* The memoized cache values. | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
protected $cache = []; | ||
|
||
/** | ||
* Create a new memoized cache instance. | ||
* | ||
* @param string $name | ||
* @param \Illuminate\Cache\Repository $repository | ||
*/ | ||
public function __construct( | ||
protected $name, | ||
protected $repository, | ||
) { | ||
// | ||
} | ||
|
||
/** | ||
* Retrieve an item from the cache by key. | ||
* | ||
* @param string $key | ||
* @return mixed | ||
*/ | ||
public function get($key) | ||
{ | ||
$prefixedKey = $this->prefix($key); | ||
|
||
if (array_key_exists($prefixedKey, $this->cache)) { | ||
return $this->cache[$prefixedKey]; | ||
} | ||
|
||
return $this->cache[$prefixedKey] = $this->repository->get($key); | ||
timacdonald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Retrieve multiple items from the cache by key. | ||
* | ||
* Items not found in the cache will have a null value. | ||
* | ||
* @return array | ||
*/ | ||
public function many(array $keys) | ||
{ | ||
[$memoized, $retrieved, $missing] = [[], [], []]; | ||
|
||
foreach ($keys as $key) { | ||
$prefixedKey = $this->prefix($key); | ||
|
||
if (array_key_exists($prefixedKey, $this->cache)) { | ||
$memoized[$key] = $this->cache[$prefixedKey]; | ||
} else { | ||
$missing[] = $key; | ||
} | ||
} | ||
|
||
if (count($missing) > 0) { | ||
$retrieved = tap($this->repository->many($missing), function ($values) { | ||
$this->cache = [ | ||
...$this->cache, | ||
...collect($values)->mapWithKeys(fn ($value, $key) => [ | ||
$this->prefix($key) => $value, | ||
]), | ||
]; | ||
}); | ||
} | ||
|
||
$result = [ | ||
...$memoized, | ||
...$retrieved, | ||
]; | ||
|
||
return array_replace(array_flip($keys), $result); | ||
} | ||
|
||
/** | ||
* Store an item in the cache for a given number of seconds. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @param int $seconds | ||
* @return bool | ||
*/ | ||
public function put($key, $value, $seconds) | ||
{ | ||
unset($this->cache[$this->prefix($key)]); | ||
|
||
return $this->repository->put($key, $value, $seconds); | ||
} | ||
|
||
/** | ||
* Store multiple items in the cache for a given number of seconds. | ||
* | ||
* @param int $seconds | ||
* @return bool | ||
*/ | ||
public function putMany(array $values, $seconds) | ||
{ | ||
foreach ($values as $key => $value) { | ||
unset($this->cache[$this->prefix($key)]); | ||
} | ||
|
||
return $this->repository->putMany($values, $seconds); | ||
} | ||
|
||
/** | ||
* Increment the value of an item in the cache. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return int|bool | ||
*/ | ||
public function increment($key, $value = 1) | ||
{ | ||
unset($this->cache[$this->prefix($key)]); | ||
|
||
return $this->repository->increment($key, $value); | ||
} | ||
|
||
/** | ||
* Decrement the value of an item in the cache. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return int|bool | ||
*/ | ||
public function decrement($key, $value = 1) | ||
{ | ||
unset($this->cache[$this->prefix($key)]); | ||
|
||
return $this->repository->decrement($key, $value); | ||
} | ||
|
||
/** | ||
* Store an item in the cache indefinitely. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function forever($key, $value) | ||
{ | ||
unset($this->cache[$this->prefix($key)]); | ||
|
||
return $this->repository->forever($key, $value); | ||
} | ||
|
||
/** | ||
* Remove an item from the cache. | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function forget($key) | ||
{ | ||
unset($this->cache[$this->prefix($key)]); | ||
|
||
return $this->repository->forget($key); | ||
} | ||
|
||
/** | ||
* Remove all items from the cache. | ||
* | ||
* @return bool | ||
*/ | ||
public function flush() | ||
{ | ||
$this->cache = []; | ||
|
||
return $this->repository->flush(); | ||
} | ||
|
||
/** | ||
* Get the cache key prefix. | ||
* | ||
* @return string | ||
*/ | ||
public function getPrefix() | ||
{ | ||
return $this->repository->getPrefix(); | ||
} | ||
|
||
/** | ||
* Prefix the given key. | ||
* | ||
* @param string $key | ||
* @return string | ||
*/ | ||
protected function prefix($key) | ||
{ | ||
return $this->getPrefix().$key; | ||
} | ||
} |
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.
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.
We bind the memo drivers to the container as scoped instances so they are refreshed in Octane and the queue worker.