Skip to content

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

Open
patel-vansh wants to merge 8 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 8 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
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant