Skip to content

Commit

Permalink
Add $ttl argument to apcu_inc() and apcu_dec()
Browse files Browse the repository at this point in the history
This if the TTL that will be used when apcu_inc/apcu_dec are called
on a non-existing (or hard expired) entry.
  • Loading branch information
nikic committed Apr 8, 2018
1 parent 818330b commit 75d5c28
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions apc_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_apcu_inc, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, step)
ZEND_ARG_INFO(1, success)
ZEND_ARG_INFO(0, ttl)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_apcu_cas, 0)
Expand Down
5 changes: 2 additions & 3 deletions apc_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ PHP_APCU_API zend_bool apc_cache_exists(apc_cache_t* cache, zend_string *key, ti
/* {{{ apc_cache_update */
PHP_APCU_API zend_bool apc_cache_update(
apc_cache_t *cache, zend_string *key, apc_cache_updater_t updater, void *data,
zend_bool insert_if_not_found)
zend_bool insert_if_not_found, zend_long ttl)
{
apc_cache_entry_t **entry;

Expand Down Expand Up @@ -990,8 +990,7 @@ PHP_APCU_API zend_bool apc_cache_update(
/* We do not check the return value of the exclusive-store (add), as the entry might have
* been added between the cache unlock and the store call. In this case we just want to
* update the entry created by a different process. */
/* TODO Always uses TTL 0 */
apc_cache_store(cache, key, &val, 0, 1);
apc_cache_store(cache, key, &val, ttl, 1);

/* Only attempt to perform insertion once. */
insert_if_not_found = 0;
Expand Down
2 changes: 1 addition & 1 deletion apc_cache_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ PHP_APCU_API zend_bool apc_cache_store(
*/
PHP_APCU_API zend_bool apc_cache_update(
apc_cache_t *cache, zend_string *key, apc_cache_updater_t updater, void *data,
zend_bool insert_if_not_found);
zend_bool insert_if_not_found, zend_long ttl);

/*
* apc_cache_find searches for a cache entry by its hashed identifier,
Expand Down
3 changes: 2 additions & 1 deletion package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
- Respect TTL when calculating APCuIterator totals.
- The per-entry TTL now always takes precedence over the global TTL.
- The global TTL is now always relative to the access time.
- apc_inc() and apc_dec() no longer update hard-expired entries. Instead a new entry is created.
- apcu_inc() and apcu_dec() no longer update hard-expired entries. Instead a new entry is created.
- Added optional $ttl argument to apcu_inc() and apcu_dec(), used when creating a new entry.
- PHP bug #76145: Fix use of APCu inside Serializer::(un)serialize().
- gh#304: If apcu_cas() is used on a non-existing entry, don't insert it.
- gh#295: Improve APCuIterator performance by using PCRE JIT and preallocating key strings.
Expand Down
23 changes: 12 additions & 11 deletions php_apc.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ PHP_FUNCTION(apcu_sma_info)

/* {{{ php_apc_update */
int php_apc_update(
zend_string *key, apc_cache_updater_t updater, void *data, zend_bool insert_if_not_found)
zend_string *key, apc_cache_updater_t updater, void *data,
zend_bool insert_if_not_found, time_t ttl)
{
if (!APCG(enabled)) {
return 0;
Expand All @@ -426,7 +427,7 @@ int php_apc_update(
apc_cache_serializer(apc_user_cache, APCG(serializer_name));
}

if (!apc_cache_update(apc_user_cache, key, updater, data, insert_if_not_found)) {
if (!apc_cache_update(apc_user_cache, key, updater, data, insert_if_not_found, ttl)) {
return 0;
}

Expand Down Expand Up @@ -530,15 +531,15 @@ static zend_bool php_inc_updater(apc_cache_t* cache, apc_cache_entry_t* entry, v
return 0;
}

/* {{{ proto long apcu_inc(string key [, long step [, bool& success]])
/* {{{ proto long apcu_inc(string key [, long step [, bool& success [, long ttl]]])
*/
PHP_FUNCTION(apcu_inc) {
zend_string *key;
struct php_inc_updater_args args;
zend_long step = 1;
zend_long step = 1, ttl = 0;
zval *success = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lz", &key, &step, &success) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lzl", &key, &step, &success, &ttl) == FAILURE) {
return;
}

Expand All @@ -549,7 +550,7 @@ PHP_FUNCTION(apcu_inc) {

ZVAL_LONG(&args.step, step);

if (php_apc_update(key, php_inc_updater, &args, 1)) {
if (php_apc_update(key, php_inc_updater, &args, 1, ttl)) {
if (success) {
ZVAL_TRUE(success);
}
Expand All @@ -564,15 +565,15 @@ PHP_FUNCTION(apcu_inc) {
}
/* }}} */

/* {{{ proto long apcu_dec(string key [, long step [, bool &success]])
/* {{{ proto long apcu_dec(string key [, long step [, bool &success [, long ttl]]])
*/
PHP_FUNCTION(apcu_dec) {
zend_string *key;
struct php_inc_updater_args args;
zend_long step = 1;
zend_long step = 1, ttl = 0;
zval *success = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lz", &key, &step, &success) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lzl", &key, &step, &success, &ttl) == FAILURE) {
return;
}

Expand All @@ -583,7 +584,7 @@ PHP_FUNCTION(apcu_dec) {

ZVAL_LONG(&args.step, 0 - step);

if (php_apc_update(key, php_inc_updater, &args, 1)) {
if (php_apc_update(key, php_inc_updater, &args, 1, ttl)) {
if (success) {
ZVAL_TRUE(success);
}
Expand Down Expand Up @@ -626,7 +627,7 @@ PHP_FUNCTION(apcu_cas) {
return;
}

if (php_apc_update(key, php_cas_updater, &vals, 0)) {
if (php_apc_update(key, php_cas_updater, &vals, 0, 0)) {
RETURN_TRUE;
}

Expand Down
46 changes: 46 additions & 0 deletions tests/apc_022.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
apcu_inc/dec() TTL parameter
--SKIPIF--
<?php
require_once(__DIR__ . '/skipif.inc');
if (!function_exists('apcu_inc_request_time')) die('skip APC debug build required');
?>
--INI--
apc.enabled=1
apc.enable_cli=1
apc.use_request_time=1
apc.ttl=0
--FILE--
<?php

$ttl = 1;
apcu_store("a", 0, $ttl);
apcu_store("b", 0, $ttl);

for ($i = 0; $i < 6; $i++) {
echo "T+$i:\n";
var_dump(apcu_inc("a"));
var_dump(apcu_inc("b", 1, $success, $ttl));
apcu_inc_request_time(1);
}

?>
--EXPECT--
T+0:
int(1)
int(1)
T+1:
int(2)
int(2)
T+2:
int(1)
int(1)
T+3:
int(2)
int(2)
T+4:
int(3)
int(1)
T+5:
int(4)
int(2)

0 comments on commit 75d5c28

Please sign in to comment.