feat: Add support for callable TTLs in cache handlers#10159
Open
patel-vansh wants to merge 14 commits into
Open
feat: Add support for callable TTLs in cache handlers#10159patel-vansh wants to merge 14 commits into
patel-vansh wants to merge 14 commits into
Conversation
memleakd
reviewed
May 6, 2026
memleakd
reviewed
May 6, 2026
7a87643 to
46b0fa7
Compare
michalsn
reviewed
May 7, 2026
Co-authored-by: Michal Sniatala <michal@sniatala.pl>
Contributor
Author
|
Both failing checks are unrelated to the changes in this PR. |
michalsn
reviewed
May 7, 2026
Co-authored-by: Michal Sniatala <michal@sniatala.pl>
michalsn
approved these changes
May 8, 2026
paulbalandan
reviewed
May 9, 2026
Member
paulbalandan
left a comment
There was a problem hiding this comment.
Last fix and we're good.
michalsn
reviewed
May 16, 2026
| <?php | ||
|
|
||
| // Simple dynamic TTL | ||
| $cache->remember('key', static fn () => 60, static fn () => fetchData()); |
Member
There was a problem hiding this comment.
Suggested change
| $cache->remember('key', static fn () => 60, static fn () => fetchData()); | |
| $cache->remember('key', static fn ($value) => 60, static fn () => fetchData()); |
|
|
||
| When a callable is provided, it will only be executed on a cache miss, | ||
| after the callback has been invoked. The callable may optionally accept | ||
| the computed value as its first argument: |
Member
There was a problem hiding this comment.
Considering changes made, shouldn't this be:
The callable always receives the computed value as its first argument
Contributor
Author
There was a problem hiding this comment.
Actually, my initial thought was to support both 0 parameters and 1 parameter callables. The recent changes was just more of a refactor and avoiding the use of Reflection class, not a restriction to always having first argument in callable. That's why the current user guide and docs explicitly states that both cases are valid. But if its misleading, I will change it.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR adds support for callable for TTL in cache handlers.
Many times the following two cases arise while calculating TTLs:
In the first senario, calculating TTL everytime, even if its a cache hit, is redundant. And in second senario, you can't just use the
remember()method. You have to rely on traditionalget()/save()methods.This PR solves both problems by adding support for callables as TTL. The passed callable may optionally accept the computed value as first parameter which solves the second senario.
And by passing callable without any parameter just defers calculating TTL until cache miss.
This can be considered as a Breaking Change as the Interface method's signature is changed. However, the usage of
remember()method remains mostly unchanged.Checklist: