Skip to content

Commit 494f2c1

Browse files
committed
Added support for TOUCH.
memcached_touch($key, $expiration = 0); memcached_touch_by_key($key, $server_key, $expiration = 0); $m->touch($key, $expiration = 0); $m->touchByKey($key, $expiration = 0); Throws an E_WARNING if called on connections with the text protocol.
1 parent db2fc78 commit 494f2c1

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

Diff for: memcached-api.php

+4
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ public function fetchAll( ) {}
201201

202202
public function set( $key, $value, $expiration = 0 ) {}
203203

204+
public function touch( $key, $expiration = 0 ) {}
205+
206+
public function touchbyKey( $key, $expiration = 0 ) {}
207+
204208
public function setByKey( $server_key, $key, $value, $expiration = 0 ) {}
205209

206210
public function setMulti( array $items, $expiration = 0 ) {}

Diff for: php_memcached.c

+57
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ typedef struct {
198198

199199
enum {
200200
MEMC_OP_SET,
201+
MEMC_OP_TOUCH,
201202
MEMC_OP_ADD,
202203
MEMC_OP_REPLACE,
203204
MEMC_OP_APPEND,
@@ -1090,6 +1091,24 @@ PHP_METHOD(Memcached, setByKey)
10901091
}
10911092
/* }}} */
10921093

1094+
/* {{{ Memcached::touch(string key, [, int expiration ])
1095+
Sets a new expiration for the given key */
1096+
PHP_METHOD(Memcached, touch)
1097+
{
1098+
php_memc_store_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, MEMC_OP_TOUCH, 0);
1099+
}
1100+
/* }}} */
1101+
1102+
/* {{{ Memcached::touchbyKey(string key, [, int expiration ])
1103+
Sets a new expiration for the given key */
1104+
PHP_METHOD(Memcached, touchByKey)
1105+
{
1106+
php_memc_store_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, MEMC_OP_TOUCH, 1);
1107+
}
1108+
/* }}} */
1109+
1110+
1111+
10931112
/* {{{ Memcached::setMulti(array items [, int expiration ])
10941113
Sets the keys/values specified in the items array */
10951114
PHP_METHOD(Memcached, setMulti)
@@ -1274,6 +1293,11 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool
12741293
INIT_ZVAL(s_zvalue);
12751294
value = &s_zvalue;
12761295
ZVAL_STRINGL(value, s_value, s_value_len, 0);
1296+
} else if (op == MEMC_OP_TOUCH) {
1297+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &server_key,
1298+
&server_key_len, &key, &key_len, &expiration) == FAILURE) {
1299+
return;
1300+
}
12771301
} else {
12781302
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz|l", &server_key,
12791303
&server_key_len, &key, &key_len, &value, &expiration) == FAILURE) {
@@ -1289,6 +1313,11 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool
12891313
INIT_ZVAL(s_zvalue);
12901314
value = &s_zvalue;
12911315
ZVAL_STRINGL(value, s_value, s_value_len, 0);
1316+
} else if (op == MEMC_OP_TOUCH) {
1317+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key,
1318+
&key_len, &expiration) == FAILURE) {
1319+
return;
1320+
}
12921321
} else {
12931322
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &key, &key_len,
12941323
&value, &expiration) == FAILURE) {
@@ -1318,6 +1347,11 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool
13181347
flags |= MEMC_VAL_COMPRESSED;
13191348
}
13201349

1350+
if (op == MEMC_OP_TOUCH && !memcached_behavior_get(m_obj->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) {
1351+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "touch is only supported with binary protocol");
1352+
RETURN_FALSE;
1353+
}
1354+
13211355
payload = php_memc_zval_to_payload(value, &payload_len, &flags, m_obj->serializer, m_obj->compression_type TSRMLS_CC);
13221356
if (payload == NULL) {
13231357
i_obj->rescode = MEMC_RES_PAYLOAD_FAILURE;
@@ -1334,6 +1368,16 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool
13341368
}
13351369
break;
13361370

1371+
case MEMC_OP_TOUCH:
1372+
if (!server_key) {
1373+
status = memcached_touch(m_obj->memc, key, key_len, expiration);
1374+
} else {
1375+
status = memcached_touch_by_key(m_obj->memc, server_key, server_key_len, key,
1376+
key_len, expiration);
1377+
}
1378+
break;
1379+
1380+
13371381
case MEMC_OP_ADD:
13381382
if (!server_key) {
13391383
status = memcached_add(m_obj->memc, key, key_len, payload, payload_len, expiration, flags);
@@ -3113,6 +3157,17 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_setByKey, 0, 0, 3)
31133157
ZEND_ARG_INFO(0, expiration)
31143158
ZEND_END_ARG_INFO()
31153159

3160+
ZEND_BEGIN_ARG_INFO_EX(arginfo_touch, 0, 0, 2)
3161+
ZEND_ARG_INFO(0, key)
3162+
ZEND_ARG_INFO(0, expiration)
3163+
ZEND_END_ARG_INFO()
3164+
3165+
ZEND_BEGIN_ARG_INFO_EX(arginfo_touchByKey, 0, 0, 3)
3166+
ZEND_ARG_INFO(0, server_key)
3167+
ZEND_ARG_INFO(0, key)
3168+
ZEND_ARG_INFO(0, expiration)
3169+
ZEND_END_ARG_INFO()
3170+
31163171
ZEND_BEGIN_ARG_INFO_EX(arginfo_setMulti, 0, 0, 1)
31173172
ZEND_ARG_ARRAY_INFO(0, items, 0)
31183173
ZEND_ARG_INFO(0, expiration)
@@ -3317,6 +3372,8 @@ static zend_function_entry memcached_class_methods[] = {
33173372

33183373
MEMC_ME(set, arginfo_set)
33193374
MEMC_ME(setByKey, arginfo_setByKey)
3375+
MEMC_ME(touch, arginfo_touch)
3376+
MEMC_ME(touchByKey, arginfo_touchByKey)
33203377
MEMC_ME(setMulti, arginfo_setMulti)
33213378
MEMC_ME(setMultiByKey, arginfo_setMultiByKey)
33223379

Diff for: tests/expire.phpt

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
--TEST--
2-
Memcached store & fetch expired key
2+
Memcached store, fetch & touch expired key
33
--SKIPIF--
44
<?php if (!extension_loaded("memcached")) print "skip"; ?>
55
--FILE--
66
<?php
77
$m = new Memcached();
8+
$m->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
89
$m->addServer('127.0.0.1', 11211, 1);
910

1011
$set = $m->set('will_expire', "foo", 2);
1112
$v = $m->get('will_expire');
1213
if (!$set || $v != 'foo') {
1314
echo "Error setting will_expire to \"foo\" with 2s expiry.\n";
1415
}
16+
sleep(1);
17+
$res = $m->touch('will_expire', 2);
18+
$v = $m->get('will_expire');
19+
if(!$res || $v != 'foo') {
20+
echo "Error touching will_expire for another 2s expiry.\n";
21+
var_dump($res);
22+
var_dump($m->getResultMessage());
23+
var_dump($v);
24+
}
25+
1526
sleep(3);
1627
$v = $m->get('will_expire');
1728

@@ -22,7 +33,18 @@ if ($v !== Memcached::GET_ERROR_RETURN_VALUE) {
2233
var_dump($v);
2334
}
2435

36+
// test with plaintext proto should throw error
37+
$m = new Memcached();
38+
$m->addServer('127.0.0.1', 11211, 1);
39+
40+
$set = $m->set('will_expire', "foo", 2);
41+
$v = $m->touch('will_expire');
42+
if($v !== false) {
43+
echo "Touch with text protocol should return false.\n";
44+
}
45+
2546
echo "OK\n";
2647
?>
27-
--EXPECT--
48+
--EXPECTF--
49+
Warning: Memcached::touch(): touch is only supported with binary protocol in %s on line %d
2850
OK

0 commit comments

Comments
 (0)