public function testDecrementSuccess() { $request = new MemcacheIncrementRequest(); $request->setKey("widgets_key"); $request->setDelta(-5); $request->setInitialValue(500); $response = new MemcacheIncrementResponse(); $response->setNewValue(7); $this->apiProxyMock->expectCall('memcache', 'Increment', $request, $response); $memcached = new Memcached(); $memcached->setOption(Memcached::OPT_PREFIX_KEY, "widgets_"); $this->assertEquals(7, $memcached->decrement("key", 5, 500, 30)); $this->assertEquals($memcached->getResultCode(), Memcached::RES_SUCCESS); $this->apiProxyMock->verify(); }
/** * Increments a numeric item's value by the specified offset. If the item's * value is not numeric, and error will result. * * @param string $key The key of the item to increment * @param int $offset The amount by which to increment the item's value * @param int $initial_value The value to set the item to if it doesn't exist. * @param int $expiry The expiry time to set on the item. * * @return The new item's value on success or false on failure. */ public function increment($key, $offset = 1, $initial_value = 0, $expiry = 0) { // Sending of a key of 'null' or an unset value is a failure. if (is_null($key)) { return false; } $key = $this->getPrefixKey($key); $request = new MemcacheIncrementRequest(); $response = new MemcacheIncrementResponse(); $request->setKey($key); $request->setDelta($offset); $request->setInitialValue($initial_value); try { ApiProxy::makeSyncCall('memcache', 'Increment', $request, $response); } catch (Error $e) { $this->result_code = self::RES_FAILURE; return false; } if ($response->hasNewValue()) { $this->result_code = self::RES_SUCCESS; return $response->getNewValue(); } else { $this->result_code = self::RES_NOTSTORED; return false; } }