Skip to content

Commit f9f40bf

Browse files
trickyTeddy Grenman
authored andcommitted
Add change of expiration time in cache callback.
Preliminary support for changing of the item expiration in the cache callback. Function signture is now: user_info_cb($memc, $key, &$value[, &$expiration = 0])
1 parent 66c9bef commit f9f40bf

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

php_memcached.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,38 +2622,51 @@ static memcached_return php_memc_do_cache_callback(zval *zmemc_obj, zend_fcall_i
26222622
{
26232623
char *payload = NULL;
26242624
size_t payload_len = 0;
2625-
zval **params[3];
2625+
zval **params[4];
26262626
zval *retval;
26272627
zval *z_key;
2628+
zval *z_expiration;
2629+
26282630
uint32_t flags = 0;
26292631
memcached_return rc;
26302632
php_memc_t* i_obj;
26312633
memcached_return status = MEMCACHED_SUCCESS;
26322634
int result;
26332635

26342636
MAKE_STD_ZVAL(z_key);
2637+
MAKE_STD_ZVAL(z_expiration);
26352638
ZVAL_STRINGL(z_key, key, key_len, 1);
26362639
ZVAL_NULL(value);
2640+
ZVAL_LONG(z_expiration, 0);
26372641

26382642
params[0] = &zmemc_obj;
26392643
params[1] = &z_key;
26402644
params[2] = &value;
2645+
params[3] = &z_expiration;
26412646

26422647
fci->retval_ptr_ptr = &retval;
26432648
fci->params = params;
2644-
fci->param_count = 3;
2649+
fci->param_count = sizeof(params) / sizeof(params[0]);
26452650

26462651
result = zend_call_function(fci, fcc TSRMLS_CC);
26472652
if (result == SUCCESS && retval) {
26482653
i_obj = (php_memc_t *) zend_object_store_get_object(zmemc_obj TSRMLS_CC);
26492654
struct memc_obj *m_obj = i_obj->obj;
26502655

26512656
if (zend_is_true(retval)) {
2657+
time_t expiration;
2658+
2659+
if (Z_TYPE_P(z_expiration) != IS_LONG) {
2660+
convert_to_long(z_expiration);
2661+
}
2662+
2663+
expiration = Z_LVAL_P(z_expiration);
2664+
26522665
payload = php_memc_zval_to_payload(value, &payload_len, &flags, m_obj->serializer TSRMLS_CC);
26532666
if (payload == NULL) {
26542667
status = (memcached_return)MEMC_RES_PAYLOAD_FAILURE;
26552668
} else {
2656-
rc = memcached_set(m_obj->memc, key, key_len, payload, payload_len, 0, flags);
2669+
rc = memcached_set(m_obj->memc, key, key_len, payload, payload_len, expiration, flags);
26572670
if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED) {
26582671
status = rc;
26592672
}
@@ -2679,6 +2692,7 @@ static memcached_return php_memc_do_cache_callback(zval *zmemc_obj, zend_fcall_i
26792692
}
26802693

26812694
zval_ptr_dtor(&z_key);
2695+
zval_ptr_dtor(&z_expiration);
26822696

26832697
return status;
26842698
}

tests/experimental/cachecallback.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ global $runs;
88

99
$runs = 0;
1010

11-
function the_callback(Memcached $memc, $key, &$value) {
11+
function the_callback(Memcached $memc, $key, &$value, &$expiration) {
1212
global $runs;
1313

1414
echo "Miss\n";
1515
var_dump($key);
1616
var_dump($value);
1717

18+
$expiration = "10";
19+
1820
$runs++;
1921
if ($runs == 1) {
2022
$value = "giving exception";
@@ -40,6 +42,7 @@ try {
4042
echo $php_errormsg, "\n";
4143
echo $e->getMessage(), "\n";
4244
}
45+
error_reporting(E_ALL);
4346
$v = $m->get('foo', 'the_callback');
4447
var_dump($v);
4548
$v = $m->get('foo', 'the_callback');

0 commit comments

Comments
 (0)