public function testDecrementSuccess() { $memcache = new Memcache(); $request = new MemcacheIncrementRequest(); $request->setKey("key"); $request->setDelta(4); $request->setDirection(MemcacheIncrementRequest\Direction::DECREMENT); $response = new MemcacheIncrementResponse(); $response->setNewValue(8); $this->apiProxyMock->expectCall('memcache', 'Increment', $request, $response); $this->assertEquals(8, memcache_decrement($memcache, "key", 4)); $this->apiProxyMock->verify(); }
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(); }
/** * Internal implementation of increment (and decrement). * * @param string $key The key associated with the value to increment. * * @param int $value The amount to increment the value. * * @param bool $is_incr Whether to perform an increment or decrement. * * @return mixed On success, the new value of the item is returned. On * failure, false is returned. */ private function incrementInternal($key, $value, $is_incr) { // Sending of a key of 'null' or an unset value is a failure. if (is_null($key)) { return false; } $request = new MemcacheIncrementRequest(); $response = new MemcacheIncrementResponse(); $request->setKey($key); $request->setDelta($value); if (!$is_incr) { $request->setDirection(MemcacheIncrementRequest\Direction::DECREMENT); } try { ApiProxy::makeSyncCall('memcache', 'Increment', $request, $response); } catch (Exception $e) { return false; } if ($response->hasNewValue()) { return $response->getNewValue(); } else { return false; } }
/** * 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; } }
public function testIncrementNonExistingValue() { $memcache = new Memcache(); $request = new MemcacheIncrementRequest(); $request->setKey("key"); $request->setDelta(5); $response = new MemcacheIncrementResponse(); $this->apiProxyMock->expectCall('memcache', 'Increment', $request, $response); $this->assertFalse(memcache_increment($memcache, "key", 5)); $this->apiProxyMock->verify(); }
/** * Increments a cached item's value. The value must be a int, float or string * representing an integer e.g. 5, 5.0 or "5" or the call with fail. * * @param string $key The key associated with the value to decrement. * * @param int $value The amount to increment the value. * * @return mixed On success, the new value of the item is returned. On * failure, false is returned. */ public function increment($key, $value = 1) { // Sending of a key of 'null' or an unset value is a failure. if (is_null($key)) { return false; } $request = new MemcacheIncrementRequest(); $response = new MemcacheIncrementResponse(); $request->setKey($key); $request->setDelta($value); try { ApiProxy::makeSyncCall('memcache', 'Increment', $request, $response); } catch (Exception $e) { return false; } if ($response->hasNewValue()) { return $response->getNewValue(); } else { return false; } }