Skip to content
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

Feature/apcu_set_ttl Add function to set the TTL of an existing entry atomically #507

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

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.

@@ -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) {
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)?

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.

2 participants