Skip to content

feat: Add support for callables for TTL in Cache Handlers#10159

Open
patel-vansh wants to merge 12 commits intocodeigniter4:4.8from
patel-vansh:feat/lazy-cache-ttl
Open

feat: Add support for callables for TTL in Cache Handlers#10159
patel-vansh wants to merge 12 commits intocodeigniter4:4.8from
patel-vansh:feat/lazy-cache-ttl

Conversation

@patel-vansh
Copy link
Copy Markdown
Contributor

Description
This PR adds support for callable for TTL in cache handlers.

Many times the following two cases arise while calculating TTLs:

  • Calculating TTL is expensive operation
  • TTL depends on the return value of the callback function (for e.g, cache until the first item's expires_at is reached).

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 traditional get()/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:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label May 5, 2026
Comment thread user_guide_src/source/changelogs/v4.8.0.rst Outdated
Comment thread system/Cache/Handlers/ApcuHandler.php
@patel-vansh patel-vansh force-pushed the feat/lazy-cache-ttl branch from 7a87643 to 46b0fa7 Compare May 7, 2026 05:13
@michalsn michalsn added breaking change Pull requests that may break existing functionalities enhancement PRs that improve existing functionalities labels May 7, 2026
Comment thread user_guide_src/source/changelogs/v4.8.0.rst Outdated
Co-authored-by: Michal Sniatala <michal@sniatala.pl>
@patel-vansh
Copy link
Copy Markdown
Contributor Author

Both failing checks are unrelated to the changes in this PR.

@patel-vansh patel-vansh requested review from memleakd and michalsn May 7, 2026 15:04
Comment thread user_guide_src/source/changelogs/v4.8.0.rst Outdated
Co-authored-by: Michal Sniatala <michal@sniatala.pl>
@patel-vansh patel-vansh requested a review from michalsn May 8, 2026 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch. breaking change Pull requests that may break existing functionalities enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants