Skip to content

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

Open
patel-vansh wants to merge 10 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 10 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
**NOTE:** If you've implemented your own classes that implement these interfaces from scratch, you will need to
update your implementations to include the new methods or method changes to ensure compatibility.

- **Cache:** ``CodeIgniter\Cache\CacheInterface::remember()`` now accepts a TTL callable. All built-in cache handlers inherit this method via ``BaseHandler``, so no changes are required for them.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **Cache:** ``CodeIgniter\Cache\CacheInterface::remember()`` now accepts a TTL callable. All built-in cache handlers inherit this method via ``BaseHandler``, so no changes are required for them.
- **Cache:** ``CodeIgniter\Cache\CacheInterface::remember()`` now accepts a TTL callable. Custom implementations of ``CacheInterface`` must update the ``$ttl`` parameter type from ``int`` to `callable|int``.

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