Skip to content

Conversation

@ignas2526
Copy link

APCu lacks a nice way to update the TTL of an entry. Currently, you'd have to implement your own critical section, fetch the value of an entry, and then store it back. I think there are many use cases with websites where one would want to create a cache entry with a short life and then extend its lifespan on a subsequent request from the browser.

I think this will be especially useful in combination with apcu_inc, apcu_dec, and apcu_cas. Because calling apcu_store (the only way to extend TTL ATM) after them defeats their purpose.

This PR adds apcu_set_ttl($key, int $ttl = 0): bool. If entry with the $key has been successfully updated, function returns true, else, false.

Copy link
Collaborator

@nikic nikic left a comment

Choose a reason for hiding this comment

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

The feature sounds reasonable to me. This is currently missing a test though.


/* {{{ apc_cache_set_ttl */
PHP_APCU_API zend_bool apc_cache_update_ttl(
apc_cache_t* cache, zend_string *key, const int32_t ttl) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
apc_cache_t* cache, zend_string *key, const int32_t ttl) {
apc_cache_t *cache, zend_string *key, int32_t ttl) {

Z_PARAM_LONG(ttl)
ZEND_PARSE_PARAMETERS_END();

t = apc_time();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unused?

PHP_FUNCTION(apcu_set_ttl) {
zend_string *key;
zend_long ttl = 0L;
zval *success = NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unused?

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(key)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(ttl)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is ttl optional?

return 0;
}

entry->ctime = t;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, should this really be setting ctime and not mtime (or neither)?

Copy link
Contributor

Choose a reason for hiding this comment

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

@nikic
I'm wondering if it wouldn't be generally better to change the behavior of apc_cache_entry_hard_expired() and apc_iterator_check_expiry() to check entries based on mtime instead of ctime.

In my opinion, it makes sense to calculate the TTL from the time of the last modification. Otherwise, you change something just before expiration, and then the entry is gone. That doesn't make sense to me.

Copy link
Author

Choose a reason for hiding this comment

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

So, my idea was to have a way to extend TTL of an entry. The only way to do it is to either change ctime or mtime as far as I understand. I don't think we should add new timestamp to track lifespan and make things complicated.

If I remember correctly, my idea was to do what apcu_add/apcu_store would do if it was called with the same values.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is not my plan to introduce a new timestamp. But ctime should imho not be changed because it is the creation timestamp and not the modification timestamp. So my plan is to refresh mtime. But this currently makes setting ttl work not as expected as you would have to increase ttl with each ttl-update if you just plan to restart timer withe same ttl. The Solution is to change all expiery checks to mtime, which imho generally makes sense...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Using mtime sounds reasonable to me.

@madmajestro
Copy link
Contributor

@ignas2526
Since this PR is inactive and I also find this feature useful, I would like to ask if you mind if I open a new PR for this?

@ignas2526
Copy link
Author

ignas2526 commented Apr 25, 2025

@madmajestro

Yea sure, you can take over if you have time. I think this PR is mostly done and the idea is outlined already. So you should have everything to wrap it up.

@madmajestro
Copy link
Contributor

@ignas2526
Thanks for your reply and your work, as it is mostly done! I will then try to provide a PR for this as soon as i have time for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants