-
Notifications
You must be signed in to change notification settings - Fork 202
Feature/apcu_set_ttl Add function to set the TTL of an existing entry atomically #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
nikic
left a comment
There was a problem hiding this 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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
|
@ignas2526 |
|
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. |
|
@ignas2526 |
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.