Skip to content

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

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

- **Database:** ``CodeIgniter\Database\ConnectionInterface`` now requires the ``afterCommit()``, ``afterRollback()``, ``inTransaction()``, and ``transaction()`` methods.
- **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.
- **Database:** ``CodeIgniter\Database\ConnectionInterface`` now requires the ``afterCommit()``, ``afterRollback()``, and ``transaction()`` methods.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks like an accidental removal of inTransaction() from the database interface change note.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, I was actually rebasing so, maybe this removal would have taken place. I'll sync it with latest file tomorrow.

Comment on lines +62 to +64
if (is_callable($ttl)) {
return parent::remember($key, $ttl, $callback);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems to prefix the APCu key twice when the TTL is callable.

With a cache prefix configured, the value gets saved under one key but normal reads look for another, so the cached value cannot be found afterward.

Could the callable TTL path keep the original key and only apply the prefix in the apcu_entry() path?

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.

2 participants