예제 #1
0
 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();
 }
예제 #2
0
 /**
  * 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;
     }
 }
예제 #3
0
 public function testDecrementSuccess()
 {
     $request = new MemcacheIncrementRequest();
     $request->setKey("widgets_key");
     $request->setDelta(5);
     $request->setDirection(MemcacheIncrementRequest\Direction::DECREMENT);
     $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();
 }
예제 #4
0
 /**
  * Internal implementation of increment (and decrement).
  *
  * @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.
  * @param bool $is_incr Whether to perform an increment or decrement.
  *
  * @return The new item's value on success or false on failure.
  */
 private function incrementInternal($key, $offset, $initial_value, $expiry, $is_incr)
 {
     // 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);
     if (!$is_incr) {
         $request->setDirection(MemcacheIncrementRequest\Direction::DECREMENT);
     }
     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;
     }
 }