Skip to content

feat: Add support for callable TTLs in cache handlers#10159

Open
patel-vansh wants to merge 14 commits into
codeigniter4:4.8from
patel-vansh:feat/lazy-cache-ttl
Open

feat: Add support for callable TTLs in cache handlers#10159
patel-vansh wants to merge 14 commits into
codeigniter4: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
@paulbalandan paulbalandan changed the title feat: Add support for callables for TTL in Cache Handlers feat: Add support for callable TTLs in cache handlers May 9, 2026
Comment thread system/Cache/Handlers/BaseHandler.php
Comment thread system/Cache/CacheInterface.php Outdated
@patel-vansh patel-vansh requested a review from paulbalandan May 15, 2026 06:06
@patel-vansh patel-vansh requested a review from michalsn May 15, 2026 06:06
Copy link
Copy Markdown
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

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

Last fix and we're good.

Comment thread system/Cache/CacheInterface.php Outdated
Copy link
Copy Markdown
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

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

Thanks!

<?php

// Simple dynamic TTL
$cache->remember('key', static fn () => 60, static fn () => fetchData());
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->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:
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.

Considering changes made, shouldn't this be:

The callable always receives the computed value as its first argument

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.

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.

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.

4 participants