Skip to content

Commit d6a43cd

Browse files
committed
Added support for incremenet/decrement ByKey
Added support for initial value initialization by increment/decrement (requires binary protocol)
1 parent f526db0 commit d6a43cd

File tree

3 files changed

+199
-26
lines changed

3 files changed

+199
-26
lines changed

php_memcached.c

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ static void php_memc_setMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_ke
307307
static void php_memc_cas_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
308308
static void php_memc_delete_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
309309
static void php_memc_deleteMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
310-
static void php_memc_incdec_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool incr);
311310
static void php_memc_getDelayed_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
312311
static memcached_return php_memc_do_cache_callback(zval *memc_obj, zend_fcall_info *fci, zend_fcall_info_cache *fcc, char *key, size_t key_len, zval *value TSRMLS_DC);
313312
static int php_memc_do_result_callback(zval *memc_obj, zend_fcall_info *fci, zend_fcall_info_cache *fcc, memcached_result_st *result TSRMLS_DC);
@@ -1598,34 +1597,27 @@ static void php_memc_deleteMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by
15981597
}
15991598
/* }}} */
16001599

1601-
/* {{{ Memcached::increment(string key [, int delta ])
1602-
Increments the value for the given key by delta, defaulting to 1 */
1603-
PHP_METHOD(Memcached, increment)
1604-
{
1605-
php_memc_incdec_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1606-
}
1607-
/* }}} */
1608-
1609-
/* {{{ Memcached::decrement(string key [, int delta ])
1610-
Decrements the value for the given key by delta, defaulting to 1 */
1611-
PHP_METHOD(Memcached, decrement)
1612-
{
1613-
php_memc_incdec_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1614-
}
1615-
/* }}} */
1616-
16171600
/* {{{ -- php_memc_incdec_impl */
1618-
static void php_memc_incdec_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool incr)
1601+
static void php_memc_incdec_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key, zend_bool incr)
16191602
{
1620-
char *key = NULL;
1621-
int key_len = 0;
1603+
char *key, *server_key;
1604+
int key_len, server_key_len;
16221605
long offset = 1;
1623-
uint64_t value;
1606+
uint64_t value, initial = 0;
1607+
time_t expiry = 0;
16241608
memcached_return status;
1609+
int n_args = ZEND_NUM_ARGS();
1610+
16251611
MEMC_METHOD_INIT_VARS;
16261612

1627-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &offset) == FAILURE) {
1628-
return;
1613+
if (!by_key) {
1614+
if (zend_parse_parameters(n_args TSRMLS_CC, "s|lll", &key, &key_len, &offset, &initial, &expiry) == FAILURE) {
1615+
return;
1616+
}
1617+
} else {
1618+
if (zend_parse_parameters(n_args TSRMLS_CC, "ss|lll", &server_key, &server_key_len, &key, &key_len, &offset, &initial, &expiry) == FAILURE) {
1619+
return;
1620+
}
16291621
}
16301622

16311623
MEMC_METHOD_FETCH_OBJECT;
@@ -1641,10 +1633,34 @@ static void php_memc_incdec_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool incr)
16411633
RETURN_FALSE;
16421634
}
16431635

1644-
if (incr) {
1645-
status = memcached_increment(m_obj->memc, key, key_len, (unsigned int)offset, &value);
1636+
if ((!by_key && n_args < 3) || (by_key && n_args < 4)) {
1637+
if (by_key) {
1638+
if (incr) {
1639+
status = memcached_increment_by_key(m_obj->memc, server_key, server_key_len, key, key_len, (unsigned int)offset, &value);
1640+
} else {
1641+
status = memcached_decrement_by_key(m_obj->memc, server_key, server_key_len, key, key_len, (unsigned int)offset, &value);
1642+
}
1643+
} else {
1644+
if (incr) {
1645+
status = memcached_increment(m_obj->memc, key, key_len, (unsigned int)offset, &value);
1646+
} else {
1647+
status = memcached_decrement(m_obj->memc, key, key_len, (unsigned int)offset, &value);
1648+
}
1649+
}
16461650
} else {
1647-
status = memcached_decrement(m_obj->memc, key, key_len, (unsigned int)offset, &value);
1651+
if (by_key) {
1652+
if (incr) {
1653+
status = memcached_increment_with_initial_by_key(m_obj->memc, server_key, server_key_len, key, key_len, (unsigned int)offset, initial, expiry, &value);
1654+
} else {
1655+
status = memcached_decrement_with_initial_by_key(m_obj->memc, server_key, server_key_len, key, key_len, (unsigned int)offset, initial, expiry, &value);
1656+
}
1657+
} else {
1658+
if (incr) {
1659+
status = memcached_increment_with_initial(m_obj->memc, key, key_len, (unsigned int)offset, initial, expiry, &value);
1660+
} else {
1661+
status = memcached_decrement_with_initial(m_obj->memc, key, key_len, (unsigned int)offset, initial, expiry, &value);
1662+
}
1663+
}
16481664
}
16491665

16501666
if (php_memc_handle_error(i_obj, status TSRMLS_CC) < 0) {
@@ -1655,6 +1671,38 @@ static void php_memc_incdec_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool incr)
16551671
}
16561672
/* }}} */
16571673

1674+
/* {{{ Memcached::increment(string key [, int delta [, initial_value [, expiry time ] ] ])
1675+
Increments the value for the given key by delta, defaulting to 1 */
1676+
PHP_METHOD(Memcached, increment)
1677+
{
1678+
php_memc_incdec_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
1679+
}
1680+
/* }}} */
1681+
1682+
/* {{{ Memcached::decrement(string key [, int delta [, initial_value [, expiry time ] ] ])
1683+
Decrements the value for the given key by delta, defaulting to 1 */
1684+
PHP_METHOD(Memcached, decrement)
1685+
{
1686+
php_memc_incdec_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
1687+
}
1688+
/* }}} */
1689+
1690+
/* {{{ Memcached::decrementByKey(string server_key, string key [, int delta [, initial_value [, expiry time ] ] ])
1691+
Decrements by server the value for the given key by delta, defaulting to 1 */
1692+
PHP_METHOD(Memcached, decrementByKey)
1693+
{
1694+
php_memc_incdec_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
1695+
}
1696+
/* }}} */
1697+
1698+
/* {{{ Memcached::increment(string server_key, string key [, int delta [, initial_value [, expiry time ] ] ])
1699+
Increments by server the value for the given key by delta, defaulting to 1 */
1700+
PHP_METHOD(Memcached, incrementByKey)
1701+
{
1702+
php_memc_incdec_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 1);
1703+
}
1704+
/* }}} */
1705+
16581706
/* {{{ Memcached::addServer(string hostname, int port [, int weight ])
16591707
Adds the given memcache server to the list */
16601708
PHP_METHOD(Memcached, addServer)
@@ -3168,11 +3216,31 @@ ZEND_END_ARG_INFO()
31683216
ZEND_BEGIN_ARG_INFO_EX(arginfo_increment, 0, 0, 1)
31693217
ZEND_ARG_INFO(0, key)
31703218
ZEND_ARG_INFO(0, offset)
3219+
ZEND_ARG_INFO(0, initial_value)
3220+
ZEND_ARG_INFO(0, expiry)
31713221
ZEND_END_ARG_INFO()
31723222

31733223
ZEND_BEGIN_ARG_INFO_EX(arginfo_decrement, 0, 0, 1)
31743224
ZEND_ARG_INFO(0, key)
31753225
ZEND_ARG_INFO(0, offset)
3226+
ZEND_ARG_INFO(0, initial_value)
3227+
ZEND_ARG_INFO(0, expiry)
3228+
ZEND_END_ARG_INFO()
3229+
3230+
ZEND_BEGIN_ARG_INFO_EX(arginfo_incrementByKey, 0, 0, 2)
3231+
ZEND_ARG_INFO(0, server_key)
3232+
ZEND_ARG_INFO(0, key)
3233+
ZEND_ARG_INFO(0, offset)
3234+
ZEND_ARG_INFO(0, initial_value)
3235+
ZEND_ARG_INFO(0, expiry)
3236+
ZEND_END_ARG_INFO()
3237+
3238+
ZEND_BEGIN_ARG_INFO_EX(arginfo_decrementByKey, 0, 0, 2)
3239+
ZEND_ARG_INFO(0, server_key)
3240+
ZEND_ARG_INFO(0, key)
3241+
ZEND_ARG_INFO(0, offset)
3242+
ZEND_ARG_INFO(0, initial_value)
3243+
ZEND_ARG_INFO(0, expiry)
31763244
ZEND_END_ARG_INFO()
31773245

31783246
ZEND_BEGIN_ARG_INFO_EX(arginfo_flush, 0, 0, 0)
@@ -3269,6 +3337,8 @@ static zend_function_entry memcached_class_methods[] = {
32693337

32703338
MEMC_ME(increment, arginfo_increment)
32713339
MEMC_ME(decrement, arginfo_decrement)
3340+
MEMC_ME(incrementByKey, arginfo_incrementByKey)
3341+
MEMC_ME(decrementByKey, arginfo_decrementByKey)
32723342

32733343
MEMC_ME(addServer, arginfo_addServer)
32743344
MEMC_ME(addServers, arginfo_addServers)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
Memcached::incrementByKey() Memcached::decrementByKey()
3+
--SKIPIF--
4+
<?php if (!extension_loaded("memcached")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$m = new Memcached();
8+
$m->addServer('localhost', 11211, 1);
9+
10+
echo "Not there\n";
11+
$m->delete('foo');
12+
var_dump($m->incrementByKey('foo', 'foo', 1));
13+
var_dump($m->decrementByKey('foo', 'foo', 1));
14+
var_dump($m->get('foo'));
15+
16+
echo "Normal\n";
17+
$m->set('foo', 1);
18+
var_dump($m->get('foo'));
19+
$m->incrementByKey('foo', 'foo');
20+
var_dump($m->get('foo'));
21+
$m->incrementByKey('foo', 'foo', 2);
22+
var_dump($m->get('foo'));
23+
$m->decrementByKey('foo', 'foo');
24+
var_dump($m->get('foo'));
25+
$m->decrementByKey('foo', 'foo', 2);
26+
var_dump($m->get('foo'));
27+
28+
error_reporting(0);
29+
echo "Invalid offset\n";
30+
$m->incrementByKey('foo', 'foo', -1);
31+
echo $php_errormsg, "\n";
32+
var_dump($m->get('foo'));
33+
$m->decrementByKey('foo', 'foo', -1);
34+
echo $php_errormsg, "\n";
35+
var_dump($m->get('foo'));
36+
37+
--EXPECT--
38+
Not there
39+
bool(false)
40+
bool(false)
41+
bool(false)
42+
Normal
43+
int(1)
44+
int(2)
45+
int(4)
46+
int(3)
47+
int(1)
48+
Invalid offset
49+
Memcached::incrementByKey(): offset has to be > 0
50+
int(1)
51+
Memcached::decrementByKey(): offset has to be > 0
52+
int(1)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
Memcached::increment() Memcached::decrement() with initial support
3+
--SKIPIF--
4+
<?php if (!extension_loaded("memcached")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$m = new Memcached();
8+
$m->setOption(Memcached::OPT_BINARY_PROTOCOL, 1);
9+
$m->addServer('localhost', 11211, 1);
10+
11+
$m->delete('foo');
12+
var_dump($m->increment('foo', 1, 1));
13+
var_dump($m->increment('foo', 0));
14+
$m->delete('foo');
15+
16+
var_dump($m->increment('foo', 1, 1));
17+
var_dump($m->increment('foo', 1, 1));
18+
var_dump($m->increment('foo', 1, 1));
19+
20+
var_dump($m->decrement('foo', 1, 1));
21+
var_dump($m->decrement('foo', 0));
22+
$m->delete('foo');
23+
24+
$m->deleteByKey('foo', 'foo');
25+
var_dump($m->incrementByKey('foo', 'foo', 1, 1));
26+
var_dump($m->incrementByKey('foo', 'foo', 0));
27+
$m->deleteByKey('foo', 'foo');
28+
29+
var_dump($m->incrementByKey('foo', 'foo', 1, 1));
30+
var_dump($m->incrementByKey('foo', 'foo', 1, 1));
31+
var_dump($m->incrementByKey('foo', 'foo', 1, 1));
32+
33+
var_dump($m->decrementByKey('foo', 'foo', 1, 1));
34+
var_dump($m->decrementByKey('foo', 'foo', 0));
35+
$m->deleteByKey('foo', 'foo');
36+
37+
--EXPECT--
38+
int(1)
39+
int(1)
40+
int(1)
41+
int(2)
42+
int(3)
43+
int(2)
44+
int(2)
45+
int(1)
46+
int(1)
47+
int(1)
48+
int(2)
49+
int(3)
50+
int(2)
51+
int(2)

0 commit comments

Comments
 (0)