diff --git a/apc_cache.c b/apc_cache.c index 095f541a..285734a7 100644 --- a/apc_cache.c +++ b/apc_cache.c @@ -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; + entry->ttl = ttl; + + apc_cache_runlock(cache); + return 1; +} /* }}} */ + #ifndef ZTS /* {{{ data_unserialize */ static zval data_unserialize(const char *filename) diff --git a/apc_cache.h b/apc_cache.h index e987a546..d0d226e5 100644 --- a/apc_cache.h +++ b/apc_cache.h @@ -151,6 +151,13 @@ PHP_APCU_API void apc_cache_clear(apc_cache_t* cache); PHP_APCU_API zend_bool apc_cache_store( apc_cache_t* cache, zend_string *key, const zval *val, const int32_t ttl, const zend_bool exclusive); + +/* + * apc_cache_update_ttl updates ttl of the key, if it exists + */ +PHP_APCU_API zend_bool apc_cache_update_ttl( + apc_cache_t* cache, zend_string *key, const int32_t ttl); + /* * apc_cache_update updates an entry in place. The updater function must not bailout. * The update is performed under write-lock and doesn't have to be atomic. diff --git a/php_apc.c b/php_apc.c index 081b62a5..d7b44016 100644 --- a/php_apc.c +++ b/php_apc.c @@ -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; + time_t t; + + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_STR(key) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(ttl) + ZEND_PARSE_PARAMETERS_END(); + + t = apc_time(); + + 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) { diff --git a/php_apc.stub.php b/php_apc.stub.php index 83830a90..9a0ce549 100644 --- a/php_apc.stub.php +++ b/php_apc.stub.php @@ -18,6 +18,8 @@ function apcu_enabled(): bool {} /** @param array|string $key */ function apcu_store($key, mixed $value = UNKNOWN, int $ttl = 0): array|bool {} +function apcu_set_ttl(string $key, int $ttl = 0): bool {} + /** @param array|string $key */ function apcu_add($key, mixed $value = UNKNOWN, int $ttl = 0): array|bool {} diff --git a/php_apc_arginfo.h b/php_apc_arginfo.h index 26c79714..47ed5301 100644 --- a/php_apc_arginfo.h +++ b/php_apc_arginfo.h @@ -24,6 +24,11 @@ ZEND_END_ARG_INFO() #define arginfo_apcu_add arginfo_apcu_store +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_apcu_set_ttl, 0, 1, _IS_BOOL, 0) + ZEND_ARG_INFO(0, key) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ttl, IS_LONG, 0, "0") +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_apcu_inc, 0, 1, MAY_BE_LONG|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, step, IS_LONG, 0, "1") @@ -69,6 +74,7 @@ PHP_APCU_API ZEND_FUNCTION(apcu_key_info); PHP_APCU_API ZEND_FUNCTION(apcu_sma_info); PHP_APCU_API ZEND_FUNCTION(apcu_enabled); PHP_APCU_API ZEND_FUNCTION(apcu_store); +PHP_APCU_API ZEND_FUNCTION(apcu_set_ttl); PHP_APCU_API ZEND_FUNCTION(apcu_add); PHP_APCU_API ZEND_FUNCTION(apcu_inc); PHP_APCU_API ZEND_FUNCTION(apcu_dec); @@ -89,6 +95,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(apcu_sma_info, arginfo_apcu_sma_info) ZEND_FE(apcu_enabled, arginfo_apcu_enabled) ZEND_FE(apcu_store, arginfo_apcu_store) + ZEND_FE(apcu_set_ttl, arginfo_apcu_set_ttl) ZEND_FE(apcu_add, arginfo_apcu_add) ZEND_FE(apcu_inc, arginfo_apcu_inc) ZEND_FE(apcu_dec, arginfo_apcu_dec)