Beispiel #1
0
 public function testGetManyAllMissing()
 {
     $memcache = new Memcache();
     $request = new MemcacheGetRequest();
     $request->addKey("key1");
     $request->addKey("key2");
     $request->addKey("key3");
     $response = new MemcacheGetResponse();
     $this->apiProxyMock->expectCall('memcache', 'Get', $request, $response);
     $this->assertFalse(memcache_get($memcache, array("key1", "key2", "key3")));
     $this->apiProxyMock->verify();
 }
 public function testTouchSuccess()
 {
     $request = new MemcacheGetRequest();
     $request->addKey("key");
     $request->setForCas(true);
     $response = new MemcacheGetResponse();
     $item = $response->addItem();
     $item->setKey("key");
     $item->setValue("value");
     $item->setFlags(0);
     // string.
     $item->setCasId(123456);
     $this->apiProxyMock->expectCall('memcache', 'Get', $request, $response);
     $request = new MemcacheSetRequest();
     $item = $request->addItem();
     $item->setKey("key");
     $item->setValue("value");
     $item->setFlags(0);
     // string
     $item->setCasId(123456);
     $item->setSetPolicy(SetPolicy::CAS);
     $item->setExpirationTime(999);
     $response = new MemcacheSetResponse();
     $response->addSetStatus(SetStatusCode::STORED);
     $this->apiProxyMock->expectCall('memcache', 'Set', $request, $response);
     $memcached = new Memcached();
     $this->assertTrue($memcached->touch("key", 999));
     $this->apiProxyMock->verify();
 }
 /**
  * Similar to Memcached::get(), but instead of a single key item, it retrieves
  * multiple items the keys of which are specified in the keys array.
  *
  * @see Memcached::get()
  *
  * @param array $keys Array of keys to retrieve.
  * @param array $cas_tokens The variable to store the CAS tokens for found
  * items.
  * @param int $flags The flags for the get operation.
  *
  * @return array The array of found items for false on failure.
  */
 public function getMulti($keys, &$cas_tokens = null, $flags = 0)
 {
     $request = new MemcacheGetRequest();
     $response = new MemcacheGetResponse();
     foreach ($keys as $key) {
         $key = $this->getPrefixKey($key);
         $request->addKey($key);
     }
     // Need to check the number of arguments passed to the function to see if
     // the user wants cas_tokens.
     if (func_num_args() > 1) {
         $request->setForCas(true);
     }
     try {
         ApiProxy::makeSyncCall('memcache', 'Get', $request, $response);
     } catch (Error $e) {
         $this->result_code = self::RES_FAILURE;
         return false;
     }
     $return_value = array();
     foreach ($response->getItemList() as $item) {
         try {
             $return_value[$item->getKey()] = MemcacheUtils::deserializeValue($item->getValue(), $item->getFlags());
         } catch (\UnexpectedValueException $e) {
             // Skip entries that cannot be deserialized.
             continue;
         }
         if ($item->hasCasId()) {
             $cas_tokens[$item->getKey()] = $item->getCasId();
         }
     }
     // If GET_PRESERVE_ORDER was set then we need to ensure that
     // a. Keys are returned in the order that they we asked for.
     // b. If a key has no value then return null for that key.
     if ($flags == self::GET_PRESERVE_ORDER) {
         $ordered_result = [];
         $ordered_cas_tokens = [];
         foreach ($keys as $key) {
             if (array_key_exists($key, $return_value)) {
                 $ordered_result[$key] = $return_value[$key];
                 if (array_key_exists($key, $cas_tokens)) {
                     $ordered_cas_tokens[$key] = $cas_tokens[$key];
                 } else {
                     $ordered_cas_tokens[$key] = null;
                 }
             } else {
                 $ordered_result[$key] = null;
                 $ordered_cas_tokens[$key] = null;
             }
         }
         $return_value = $ordered_result;
         if (func_num_args() > 1) {
             $cas_tokens = $ordered_cas_tokens;
         }
     }
     return $return_value;
 }
Beispiel #4
0
 private function getMulti($keys, $flags = null)
 {
     $request = new MemcacheGetRequest();
     $response = new MemcacheGetResponse();
     foreach ($keys as $key) {
         $request->addKey($key);
     }
     ApiProxy::makeSyncCall('memcache', 'Get', $request, $response);
     $return_value = array();
     foreach ($response->getItemList() as $item) {
         try {
             $return_value[$item->getKey()] = MemcacheUtils::deserializeValue($item->getValue(), $item->getFlags());
         } catch (\UnexpectedValueException $e) {
             // Skip entries that cannot be deserialized.
         }
     }
     return $return_value;
 }
 private function getMulti($keys, $flags = null)
 {
     $request = new MemcacheGetRequest();
     $response = new MemcacheGetResponse();
     foreach ($keys as $key) {
         $request->addKey($key);
     }
     ApiProxy::makeSyncCall('memcache', 'Get', $request, $response);
     $return_value = array();
     foreach ($response->getItemList() as $item) {
         $return_value[$item->getKey()] = MemcacheUtils::deserializeValue($item->getValue(), $item->getFlags());
     }
     return $return_value;
 }