-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -273,7 +273,7 @@ PHP_APCU_API int APC_UNSERIALIZER_NAME(php) (APC_UNSERIALIZER_ARGS) | |
| result = php_var_unserialize(value, &tmp, buf + buf_len, &var_hash); | ||
| PHP_VAR_UNSERIALIZE_DESTROY(var_hash); | ||
| BG(serialize_lock)--; | ||
|
|
||
| if (!result) { | ||
| php_error_docref(NULL, E_NOTICE, "Error at offset %td of %zd bytes", tmp - buf, buf_len); | ||
| ZVAL_NULL(value); | ||
|
|
@@ -539,6 +539,34 @@ PHP_APCU_API zend_bool apc_cache_store( | |
| return ret; | ||
| } /* }}} */ | ||
|
|
||
| /* {{{ apc_cache_set_ttl */ | ||
| PHP_APCU_API zend_bool apc_cache_update_ttl( | ||
| apc_cache_t* cache, zend_string *key, const int32_t ttl) { | ||
| apc_cache_entry_t *entry; | ||
| zend_bool retval = 0; | ||
| time_t t = apc_time(); | ||
|
|
||
| if (!cache) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (!apc_cache_rlock(cache)) { | ||
| return 0; | ||
| } | ||
|
|
||
| entry = apc_cache_rlocked_find_nostat(cache, key, t); | ||
| if (!entry) { | ||
| apc_cache_runlock(cache); | ||
| return 0; | ||
| } | ||
|
|
||
| entry->ctime = t; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, should this really be setting ctime and not mtime (or neither)?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikic 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using mtime sounds reasonable to me. |
||
| entry->ttl = ttl; | ||
|
|
||
| apc_cache_runlock(cache); | ||
| return 1; | ||
| } /* }}} */ | ||
|
|
||
| #ifndef ZTS | ||
| /* {{{ data_unserialize */ | ||
| static zval data_unserialize(const char *filename) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -517,6 +517,26 @@ PHP_FUNCTION(apcu_store) { | |
| } | ||
| /* }}} */ | ||
|
|
||
| /* {{{ proto bool apc_cache_update_ttl(mixed key [, long ttl ]) | ||
| */ | ||
| PHP_FUNCTION(apcu_set_ttl) { | ||
| zend_string *key; | ||
| zend_long ttl = 0L; | ||
| zval *success = NULL; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused? |
||
| time_t t; | ||
|
|
||
| ZEND_PARSE_PARAMETERS_START(1, 2) | ||
| Z_PARAM_STR(key) | ||
| Z_PARAM_OPTIONAL | ||
| Z_PARAM_LONG(ttl) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is ttl optional? |
||
| ZEND_PARSE_PARAMETERS_END(); | ||
|
|
||
| t = apc_time(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused? |
||
|
|
||
| RETURN_BOOL(apc_cache_update_ttl(apc_user_cache, key, ttl)); | ||
| } | ||
| /* }}} */ | ||
|
|
||
| /* {{{ proto int apcu_add(mixed key, mixed var [, long ttl ]) | ||
| */ | ||
| PHP_FUNCTION(apcu_add) { | ||
|
|
||
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.