Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 494f2c14be36187b28fb8135274abb58518f59fd 1 parent db2fc78
@janl janl authored
Showing with 85 additions and 2 deletions.
  1. +4 −0 memcached-api.php
  2. +57 −0 php_memcached.c
  3. +24 −2 tests/expire.phpt
View
4 memcached-api.php
@@ -201,6 +201,10 @@ public function fetchAll( ) {}
public function set( $key, $value, $expiration = 0 ) {}
+ public function touch( $key, $expiration = 0 ) {}
+
+ public function touchbyKey( $key, $expiration = 0 ) {}
+
public function setByKey( $server_key, $key, $value, $expiration = 0 ) {}
public function setMulti( array $items, $expiration = 0 ) {}
View
57 php_memcached.c
@@ -198,6 +198,7 @@ typedef struct {
enum {
MEMC_OP_SET,
+ MEMC_OP_TOUCH,
MEMC_OP_ADD,
MEMC_OP_REPLACE,
MEMC_OP_APPEND,
@@ -1090,6 +1091,24 @@ PHP_METHOD(Memcached, setByKey)
}
/* }}} */
+/* {{{ Memcached::touch(string key, [, int expiration ])
+ Sets a new expiration for the given key */
+PHP_METHOD(Memcached, touch)
+{
+ php_memc_store_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, MEMC_OP_TOUCH, 0);
+}
+/* }}} */
+
+/* {{{ Memcached::touchbyKey(string key, [, int expiration ])
+ Sets a new expiration for the given key */
+PHP_METHOD(Memcached, touchByKey)
+{
+ php_memc_store_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, MEMC_OP_TOUCH, 1);
+}
+/* }}} */
+
+
+
/* {{{ Memcached::setMulti(array items [, int expiration ])
Sets the keys/values specified in the items array */
PHP_METHOD(Memcached, setMulti)
@@ -1274,6 +1293,11 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool
INIT_ZVAL(s_zvalue);
value = &s_zvalue;
ZVAL_STRINGL(value, s_value, s_value_len, 0);
+ } else if (op == MEMC_OP_TOUCH) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &server_key,
+ &server_key_len, &key, &key_len, &expiration) == FAILURE) {
+ return;
+ }
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz|l", &server_key,
&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
INIT_ZVAL(s_zvalue);
value = &s_zvalue;
ZVAL_STRINGL(value, s_value, s_value_len, 0);
+ } else if (op == MEMC_OP_TOUCH) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key,
+ &key_len, &expiration) == FAILURE) {
+ return;
+ }
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &key, &key_len,
&value, &expiration) == FAILURE) {
@@ -1318,6 +1347,11 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool
flags |= MEMC_VAL_COMPRESSED;
}
+ if (op == MEMC_OP_TOUCH && !memcached_behavior_get(m_obj->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "touch is only supported with binary protocol");
+ RETURN_FALSE;
+ }
+
payload = php_memc_zval_to_payload(value, &payload_len, &flags, m_obj->serializer, m_obj->compression_type TSRMLS_CC);
if (payload == NULL) {
i_obj->rescode = MEMC_RES_PAYLOAD_FAILURE;
@@ -1334,6 +1368,16 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool
}
break;
+ case MEMC_OP_TOUCH:
+ if (!server_key) {
+ status = memcached_touch(m_obj->memc, key, key_len, expiration);
+ } else {
+ status = memcached_touch_by_key(m_obj->memc, server_key, server_key_len, key,
+ key_len, expiration);
+ }
+ break;
+
+
case MEMC_OP_ADD:
if (!server_key) {
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)
ZEND_ARG_INFO(0, expiration)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_touch, 0, 0, 2)
+ ZEND_ARG_INFO(0, key)
+ ZEND_ARG_INFO(0, expiration)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_touchByKey, 0, 0, 3)
+ ZEND_ARG_INFO(0, server_key)
+ ZEND_ARG_INFO(0, key)
+ ZEND_ARG_INFO(0, expiration)
+ZEND_END_ARG_INFO()
+
ZEND_BEGIN_ARG_INFO_EX(arginfo_setMulti, 0, 0, 1)
ZEND_ARG_ARRAY_INFO(0, items, 0)
ZEND_ARG_INFO(0, expiration)
@@ -3317,6 +3372,8 @@ static zend_function_entry memcached_class_methods[] = {
MEMC_ME(set, arginfo_set)
MEMC_ME(setByKey, arginfo_setByKey)
+ MEMC_ME(touch, arginfo_touch)
+ MEMC_ME(touchByKey, arginfo_touchByKey)
MEMC_ME(setMulti, arginfo_setMulti)
MEMC_ME(setMultiByKey, arginfo_setMultiByKey)
View
26 tests/expire.phpt
@@ -1,10 +1,11 @@
--TEST--
-Memcached store & fetch expired key
+Memcached store, fetch & touch expired key
--SKIPIF--
<?php if (!extension_loaded("memcached")) print "skip"; ?>
--FILE--
<?php
$m = new Memcached();
+$m->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$m->addServer('127.0.0.1', 11211, 1);
$set = $m->set('will_expire', "foo", 2);
@@ -12,6 +13,16 @@ $v = $m->get('will_expire');
if (!$set || $v != 'foo') {
echo "Error setting will_expire to \"foo\" with 2s expiry.\n";
}
+sleep(1);
+$res = $m->touch('will_expire', 2);
+$v = $m->get('will_expire');
+if(!$res || $v != 'foo') {
+ echo "Error touching will_expire for another 2s expiry.\n";
+ var_dump($res);
+ var_dump($m->getResultMessage());
+ var_dump($v);
+}
+
sleep(3);
$v = $m->get('will_expire');
@@ -22,7 +33,18 @@ if ($v !== Memcached::GET_ERROR_RETURN_VALUE) {
var_dump($v);
}
+// test with plaintext proto should throw error
+$m = new Memcached();
+$m->addServer('127.0.0.1', 11211, 1);
+
+$set = $m->set('will_expire', "foo", 2);
+$v = $m->touch('will_expire');
+if($v !== false) {
+ echo "Touch with text protocol should return false.\n";
+}
+
echo "OK\n";
?>
---EXPECT--
+--EXPECTF--
+Warning: Memcached::touch(): touch is only supported with binary protocol in %s on line %d
OK
Please sign in to comment.
Something went wrong with that request. Please try again.