/**
  * @param string[] $keys
  * @return mixed[]
  */
 public function multiGet(string ...$keys) : array
 {
     if (count($keys) === 0) {
         return [];
     }
     return $this->client->getMulti($keys);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function getMulti(array $keys, array &$tokens = null)
 {
     $return = $this->client->getMulti($keys, $tokens);
     // HHVMs getMulti() returns null instead of empty array for no results,
     // so normalize that
     $tokens = $tokens ?: array();
     return $return ?: array();
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 protected function getArrayDataFromStorage(array $keys)
 {
     $result = $this->memcached->getMulti($keys);
     if ($result === false) {
         return [];
     }
     return $result;
 }
Ejemplo n.º 4
0
 public function multiRead($keys)
 {
     $cas = null;
     $values = $this->store->getMulti($keys, $cas, Memcached::GET_PRESERVE_ORDER);
     if (!$values) {
         $values = array_fill_keys($keys, null);
     }
     return $values;
 }
Ejemplo n.º 5
0
 /**
  * Read values for a set of keys from cache
  *
  * @param  array $keys list of keys to fetch
  *
  * @return array   list of values with the given keys used as indexes
  * @return boolean true on success, false on failure
  */
 protected function read(array $keys)
 {
     $_keys = $lookup = [];
     list($_keys, $lookup) = $this->eachKeys($keys, $_keys, $lookup);
     $_res = [];
     $res = $this->memcached->getMulti($_keys);
     foreach ($res as $k => $v) {
         $_res[$lookup[$k]] = $v;
     }
     return $_res;
 }
Ejemplo n.º 6
0
 /**
  * Retrieve multiple items from the cache by key.
  *
  * Items not found in the cache will have a null value.
  *
  * @param  array  $keys
  * @return array
  */
 public function many(array $keys)
 {
     $prefixedKeys = array_map(function ($key) {
         return $this->prefix . $key;
     }, $keys);
     $values = $this->memcached->getMulti($prefixedKeys, null, Memcached::GET_PRESERVE_ORDER);
     if ($this->memcached->getResultCode() != 0) {
         return array_fill_keys($keys, null);
     }
     return array_combine($keys, $values);
 }
Ejemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function getMulti(array $keys, array &$tokens = null)
 {
     $keys = array_map(array($this, 'encodeKey'), $keys);
     $return = $this->client->getMulti($keys, $tokens);
     $keys = array_map(array($this, 'decodeKey'), array_keys($return));
     $return = array_combine($keys, $return);
     // HHVMs getMulti() returns null instead of empty array for no results,
     // so normalize that
     $tokens = $tokens ?: array();
     $tokens = array_combine($keys, $tokens);
     return $return ?: array();
 }
Ejemplo n.º 8
0
 /**
  * 获取多条数据记录
  * @param array $keys
  * @return mixed
  */
 public function getMulti(array $keys)
 {
     //保证返回的key的顺序和请求时一致
     $data = $this->dbh->getMulti($keys, $cas, \Memcached::GET_PRESERVE_ORDER);
     if ($data === false) {
         if ($this->dbh->getResultCode() == \Memcached::RES_NOTFOUND) {
             return array();
         } else {
             $this->error('getMulti', json_encode($keys));
         }
     }
     return $data;
 }
Ejemplo n.º 9
0
 /**
  * Get multiple values from the cache.
  *
  * @param array $keys An array of keys to get
  *
  * @return array An array of JSON objects
  */
 public function mget(array $keys = array())
 {
     $values = $this->client->getMulti($keys);
     $rtn = [];
     // The Memcached extension returns false not null for nonexistent
     // values. For consistency's sake, we spoof that here
     foreach ($keys as $key) {
         if (!isset($values[$key]) || $values[$key] === false) {
             $values[$key] = null;
         }
         $rtn[$this->instance->unkey($key)] = $values[$key];
     }
     return $rtn;
 }
Ejemplo n.º 10
0
 public function getMulti($keys)
 {
     $result = new MultiGetResult($keys);
     $null = null;
     $cached = $this->memcached->getMulti(array_keys($keys), $null, \Memcached::GET_PRESERVE_ORDER);
     foreach ($cached as $key => $value) {
         if (null === $value) {
             $id = $keys[$key];
             $result->miss($id, $key);
         } else {
             $result->hit($key, $value);
         }
     }
     return $result;
 }
Ejemplo n.º 11
0
 /**
  * Get multiple items.
  *
  * Options:
  *  - ttl <float> optional
  *    - The time-to-life (Default: ttl of object)
  *  - namespace <string> optional
  *    - The namespace to use (Default: namespace of object)
  *
  * @param  array $keys
  * @param  array $options
  * @return array Assoziative array of existing keys and values or false on failure
  * @throws Exception
  *
  * @triggers getItems.pre(PreEvent)
  * @triggers getItems.post(PostEvent)
  * @triggers getItems.exception(ExceptionEvent)
  */
 public function getItems(array $keys, array $options = array())
 {
     $baseOptions = $this->getOptions();
     if (!$baseOptions->getReadable()) {
         return array();
     }
     $this->normalizeOptions($options);
     $args = new ArrayObject(array('keys' => &$keys, 'options' => &$options));
     try {
         $eventRs = $this->triggerPre(__FUNCTION__, $args);
         if ($eventRs->stopped()) {
             return $eventRs->last();
         }
         $namespaceSep = $baseOptions->getNamespaceSeparator();
         $internalKeys = array();
         foreach ($keys as $key) {
             $internalKeys[] = $options['namespace'] . $namespaceSep . $key;
         }
         $fetch = $this->memcached->getMulti($internalKeys);
         if ($fetch === false) {
             throw new Exception\ItemNotFoundException('Memcached::getMulti(<array>) failed');
         }
         // remove namespace prefix
         $prefixL = strlen($options['namespace'] . $namespaceSep);
         $result = array();
         foreach ($fetch as $internalKey => &$value) {
             $result[substr($internalKey, $prefixL)] = $value;
         }
         return $this->triggerPost(__FUNCTION__, $args, $result);
     } catch (\Exception $e) {
         return $this->triggerException(__FUNCTION__, $args, $e);
     }
 }
Ejemplo n.º 12
0
 /**
  * @Name get 根据键名获取值
  *
  * @param string $key key
  * @return array OR json object OR string...
  *         add by cheng.yafei
  *
  */
 public function get($key = NULL)
 {
     if ($this->m) {
         $keyName = $this->key_name($key);
         if (isset($this->local_cache[$keyName])) {
             return $this->local_cache[$keyName];
         }
         if (is_null($key)) {
             $this->errors[] = 'The key value cannot be NULL';
             return false;
         }
         if (is_array($key)) {
             foreach ($key as $n => $k) {
                 $key[$n] = $this->key_name($k);
             }
             return $this->m->getMulti($key);
         } else {
             $obj = $this->m->get($keyName);
             $this->local_cache[$keyName] = $obj;
             return $obj;
         }
     } else {
         return false;
     }
 }
 /**
  * Read cached data
  *
  * @param array $keys array of keys to load
  * @return array key/value cached data
  */
 protected function read(array $keys)
 {
     $keymap = array();
     $hashedkeys = array();
     foreach ($keys as $key) {
         $newkey = sha1($key);
         $keymap[$newkey] = $key;
         $hashedkeys[] = $newkey;
     }
     $data = false;
     $cachedata = array();
     if ($this->memcacheType == GitPHP_CacheResource_Memcache::Memcache) {
         $cachedata = $this->memcacheObj->get($hashedkeys);
     } else {
         if ($this->memcacheType == GitPHP_CacheResource_Memcache::Memcached) {
             $cachedata = $this->memcacheObj->getMulti($hashedkeys);
         }
     }
     if ($cachedata) {
         foreach ($cachedata as $key => $value) {
             $origkey = $keymap[$key];
             if (!empty($origkey)) {
                 $data[$origkey] = $value;
             }
         }
     }
     return $data;
 }
Ejemplo n.º 14
0
 public function getCounters()
 {
     $counters = array();
     $result = $this->memcache->getMulti(array($this->prefix . 'hit', $this->prefix . 'miss', $this->prefix . 'error'));
     $counters['hit'] = array_key_exists($this->prefix . 'hit', $result) ? $result[$this->prefix . 'hit'] : 0;
     $counters['miss'] = array_key_exists($this->prefix . 'miss', $result) ? $result[$this->prefix . 'miss'] : 0;
     $counters['error'] = array_key_exists($this->prefix . 'error', $result) ? $result[$this->prefix . 'error'] : 0;
     $counters['age'] = $this->getReportAge();
     return $counters;
 }
Ejemplo n.º 15
0
 /**
  * Get metadata of multiple items
  *
  * @param  array $normalizedKeys
  * @return array Associative array of keys and metadata
  * @throws Exception\ExceptionInterface
  */
 protected function internalGetMetadatas(array &$normalizedKeys)
 {
     $result = $this->memcached->getMulti($normalizedKeys);
     if ($result === false) {
         throw $this->getExceptionByResultCode($this->memcached->getResultCode());
     }
     foreach ($result as &$value) {
         $value = array();
     }
     return $result;
 }
Ejemplo n.º 16
0
 /**
  * Get metadata of multiple items
  *
  * Options:
  *  - namespace <string> optional
  *    - The namespace to use
  *
  * @param  array $normalizedKeys
  * @param  array $normalizedOptions
  * @return array
  * @throws Exception\ExceptionInterface
  *
  * @triggers getMetadatas.pre(PreEvent)
  * @triggers getMetadatas.post(PostEvent)
  * @triggers getMetadatas.exception(ExceptionEvent)
  */
 protected function internalGetMetadatas(array &$normalizedKeys, array &$normalizedOptions)
 {
     $this->memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $normalizedOptions['namespace']);
     $result = $this->memcached->getMulti($normalizedKeys);
     if ($result === false) {
         throw $this->getExceptionByResultCode($this->memcached->getResultCode());
     }
     foreach ($result as $key => &$value) {
         $value = array();
     }
     return $result;
 }
Ejemplo n.º 17
0
 /**
  * Gets multiple values from memcached in one request.
  *
  * See the buildKeys method definition to understand the $keys/$groups parameters.
  *
  * @link http://www.php.net/manual/en/memcached.getmulti.php
  *
  * @param   array           $keys       Array of keys to retrieve.
  * @param   string|array    $groups     If string, used for all keys. If arrays, corresponds with the $keys array.
  * @param   string          $server_key The key identifying the server to store the value on.
  * @param   null|array      $cas_tokens The variable to store the CAS tokens for the found items.
  * @param   int             $flags      The flags for the get operation.
  * @return  bool|array                  Returns the array of found items or FALSE on failure.
  */
 public function getMulti($keys, $groups = 'default', $server_key = '', &$cas_tokens = NULL, $flags = NULL)
 {
     $derived_keys = $this->buildKeys($keys, $groups);
     /**
      * If either $cas_tokens, or $flags is set, must hit Memcached and bypass runtime cache. Note that
      * this will purposely ignore no_mc_groups values as they cannot handle CAS tokens or the special
      * flags; however, if the groups of groups contains a no_mc_group, this is bypassed.
      */
     if (func_num_args() > 3 && !$this->contains_no_mc_group($groups)) {
         if (!empty($server_key)) {
             $values = $this->mc->getMultiByKey($server_key, $derived_keys, $cas_tokens, $flags);
         } else {
             $values = $this->mc->getMulti($derived_keys, $cas_tokens, $flags);
         }
     } else {
         $values = array();
         $need_to_get = array();
         // Pull out values from runtime cache, or mark for retrieval
         foreach ($derived_keys as $key) {
             if (isset($this->cache[$key])) {
                 $values[$key] = $this->cache[$key];
             } else {
                 $need_to_get[$key] = $key;
             }
         }
         // Get those keys not found in the runtime cache
         if (!empty($need_to_get)) {
             if (!empty($server_key)) {
                 $result = $this->mc->getMultiByKey($server_key, array_keys($need_to_get));
             } else {
                 $result = $this->mc->getMulti(array_keys($need_to_get));
             }
         }
         // Merge with values found in runtime cache
         if (isset($result) && Memcached::RES_SUCCESS === $this->getResultCode()) {
             $values = array_merge($values, $result);
         }
         // If order should be preserved, reorder now
         if (!empty($need_to_get) && $flags === Memcached::GET_PRESERVE_ORDER) {
             $ordered_values = array();
             foreach ($derived_keys as $key) {
                 if (isset($values[$key])) {
                     $ordered_values[$key] = $values[$key];
                 }
             }
             $values = $ordered_values;
             unset($ordered_values);
         }
     }
     // Add the values to the runtime cache
     $this->cache = array_merge($this->cache, $values);
     return $values;
 }
Ejemplo n.º 18
0
 /**
  * Retrieves several items from the cache store in a single transaction.
  *
  * If not all of the items are available in the cache then the data value for those that are missing will be set to false.
  *
  * @param array $keys The array of keys to retrieve
  * @return array An array of items from the cache. There will be an item for each key, those that were not in the store will
  *      be set to false.
  */
 public function get_many($keys)
 {
     $result = $this->connection->getMulti($keys);
     if (!is_array($result)) {
         $result = array();
     }
     foreach ($keys as $key) {
         if (!array_key_exists($key, $result)) {
             $result[$key] = false;
         }
     }
     return $result;
 }
Ejemplo n.º 19
0
 /**
  * @param array $keys
  * @return array
  */
 protected function fetchMultiObjectsFromCache(array $keys)
 {
     $items = [];
     $result = $this->cache->getMulti($keys, $null, \Memcached::GET_PRESERVE_ORDER);
     foreach ($result as $key => $value) {
         $cacheItem = new CacheItem($key);
         if (false !== ($result = unserialize($this->cache->get($key)))) {
             $cacheItem->set($result);
         }
         $items[$key] = $cacheItem;
     }
     return $items;
 }
Ejemplo n.º 20
0
 /**
  * 批量获取键值
  * @param array $keys   键名数组
  * @return array|false  键值数组,失败返回false
  */
 public function mGet($keys)
 {
     try {
         $ret = [];
         $values = $this->handler->getMulti($keys);
         foreach ($keys as $key) {
             $ret[$key] = isset($values[$key]) ? self::getValue($values[$key]) : false;
         }
         return $ret;
     } catch (Exception $ex) {
         self::exception($ex);
         //连接状态置为false
         $this->isConnected = false;
     }
     return false;
 }
Ejemplo n.º 21
0
 /**
  * @param  array
  * @throws RunTimeException
  * @return array
  */
 public function all(array $ids)
 {
     $this->keys = array();
     $this->keys = preg_filter('/^/', $this->table . '_', $ids);
     $data = $this->Memcached->getMulti($this->keys);
     if ($data === false) {
         $msg = $this->Memcached->getResultMessage();
         $code = $this->Memcached->getResultCode();
         throw new \RunTimeException("Memcached Error (all): " . $msg . "(" . $code . ")");
     }
     $result = array();
     foreach ($data as $key => $val) {
         $result[str_replace($this->table . '_', '', $key)] = json_decode(gzuncompress($val), true);
     }
     return $result;
 }
    protected function loadValuesImpl(array $cacheEntryNames) {
        $values = $this->memcached->getMulti($cacheEntryNames);

        if ($values === FALSE) {
            if ($this->memcached->getResultCode() != Memcached::RES_NOTFOUND) {
                LogHelper::log_error(t(
                    '[@cacheType] Could not get values (@cacheEntryNames): @message',
                    array(
                        '@cacheType' => self::CACHE__TYPE,
                        '@cacheEntryNames' => implode(', ', $cacheEntryNames),
                        '@message' => $this->memcached->getResultMessage())));
            }
        }

        return $values;
    }
Ejemplo n.º 23
0
 /**
  *
  */
 public function getMulti(array $keys, &$cas_tokens = null, $flags = null)
 {
     if ($this->_noCache) {
         return array();
     }
     //        $_test = ($this->_baseKey !== false);
     //        if ($_test) {
     //            $keys = array_flip($keys);
     //            $keys = $this->_translateKeys($keys);
     //            $keys = array_keys($keys);
     //        }
     $_ret = $this->_memcached->getMulti($keys, $cas_tokens, $flags);
     if (!empty($_ret)) {
         $_ret = $this->_reverttranslation($_ret);
     }
     return $_ret;
 }
Ejemplo n.º 24
0
 /**
  * @param array $ids
  * @return array id=>object
  */
 protected function _load(array $ids = array())
 {
     if (!$this->memcached) {
         return array();
     }
     $_idsCopy = array_flip($ids);
     // Array transformation :  array[id] = id  INTO  array[memcachedId] = id
     array_walk($_idsCopy, function (&$value, $key, $context) {
         $value = $context->getMemcachedCompleteKey($key);
         // "ev:mc:obj:" . $value;
     }, $this);
     $_memcachedFormattedIds = array_flip($_idsCopy);
     // unserialized & store locally
     $_objectsFromCache = $this->memcached->getMulti(array_keys($_memcachedFormattedIds));
     $_objects = array();
     foreach ($_objectsFromCache as $_memcachedKey => &$_objectFromCache) {
         if ($_objectFromCache->getVersion() == $_objectFromCache::VERSION) {
             $_objects[$_memcachedFormattedIds[$_memcachedKey]] = $_objectFromCache;
         }
     }
     return $_objects;
 }
Ejemplo n.º 25
0
 /**
  * Get multiple items.
  *
  * Options:
  *  - namespace <string> optional
  *    - The namespace to use (Default: namespace of object)
  *
  * @param  array $keys
  * @param  array $options
  * @return array Assoziative array of existing keys and values or false on failure
  * @throws Exception
  *
  * @triggers getItems.pre(PreEvent)
  * @triggers getItems.post(PostEvent)
  * @triggers getItems.exception(ExceptionEvent)
  */
 public function getItems(array $keys, array $options = array())
 {
     $baseOptions = $this->getOptions();
     if (!$baseOptions->getReadable()) {
         return array();
     }
     $this->normalizeOptions($options);
     $args = new ArrayObject(array('keys' => &$keys, 'options' => &$options));
     try {
         $eventRs = $this->triggerPre(__FUNCTION__, $args);
         if ($eventRs->stopped()) {
             return $eventRs->last();
         }
         $this->memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $options['namespace']);
         $result = $this->memcached->getMulti($keys);
         if ($result === false) {
             throw $this->getExceptionByResultCode($this->memcached->getResultCode());
         }
         return $this->triggerPost(__FUNCTION__, $args, $result);
     } catch (\Exception $e) {
         return $this->triggerException(__FUNCTION__, $args, $e);
     }
 }
Ejemplo n.º 26
0
 /**
  * Returns the `group value` for each of the configured groups
  * If the group initial value was not found, then it initializes
  * the group accordingly.
  *
  * @return array
  */
 public function groups()
 {
     if (empty($this->_compiledGroupNames)) {
         foreach ($this->_config['groups'] as $group) {
             $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
         }
     }
     $groups = $this->_Memcached->getMulti($this->_compiledGroupNames);
     if (count($groups) !== count($this->_config['groups'])) {
         foreach ($this->_compiledGroupNames as $group) {
             if (!isset($groups[$group])) {
                 $this->_Memcached->set($group, 1, 0);
                 $groups[$group] = 1;
             }
         }
         ksort($groups);
     }
     $result = [];
     $groups = array_values($groups);
     foreach ($this->_config['groups'] as $i => $group) {
         $result[] = $group . $groups[$i];
     }
     return $result;
 }
Ejemplo n.º 27
0
 /**
  * Reads from cache in bulk.
  * @param  string key
  * @return array key => value pairs, missing items are omitted
  */
 public function bulkRead(array $keys)
 {
     $prefixedKeys = array_map(function ($key) {
         return urlencode($this->prefix . $key);
     }, $keys);
     $keys = array_combine($prefixedKeys, $keys);
     $metas = $this->memcached->getMulti($prefixedKeys);
     $result = [];
     $deleteKeys = [];
     foreach ($metas as $prefixedKey => $meta) {
         if (!empty($meta[self::META_CALLBACKS]) && !Cache::checkCallbacks($meta[self::META_CALLBACKS])) {
             $deleteKeys[] = $prefixedKey;
         } else {
             $result[$keys[$prefixedKey]] = $meta[self::META_DATA];
         }
         if (!empty($meta[self::META_DELTA])) {
             $this->memcached->replace($prefixedKey, $meta, $meta[self::META_DELTA] + time());
         }
     }
     if (!empty($deleteKeys)) {
         $this->memcached->deleteMulti($deleteKeys, 0);
     }
     return $result;
 }
             //$m->set($shareKey,0,360);
             //$m->set('share_ok',0,360);
             $items = array('share_ok' => 0, 'share_fail' => 0);
             $m->setMulti($items, time() + 360);
         } else {
             $shareData = $shareData + 1;
             $m->set($shareKey, $shareData, 360);
         }
     }
 } else {
     if ($method == 'eth_getWork') {
         $null = null;
         $shareCheckerKey = 'submiting_' . $payout_addr . '_' . $hash_rate;
         $data_multi = array('blockinfo' => 'blockinfo', 'eth_getWork_response' => 'eth_getWork_response', $shareCheckerKey => $shareCheckerKey);
         $keys = array_keys($data_multi);
         $got = $m->getMulti($keys, $null);
         while (!$got) {
             usleep(100000);
             $got = $m->getMulti($keys, $null);
         }
         $result1 = $got["blockinfo"];
         $result = $got["eth_getWork_response"];
         $key_Key = $shareCheckerKey;
         $CheckShareData = $got[$key_Key];
         //$result1 = $m->get('blockinfo');
         $block_info_last = json_decode($result1, true);
         $last_block_result = $block_info_last['result'];
         $last_block_diff = $last_block_result['difficulty'];
         $last_block_timestamp = new Math_BigInteger(hexdec($last_block_result['timestamp']));
         $current_time = new Math_BigInteger(time());
         $lastBlockTime_BI = $current_time->subtract($last_block_timestamp);
Ejemplo n.º 29
0
 public function testGetMultiUnexpectedValue()
 {
     $request = new MemcacheGetRequest();
     $request->addKey("key");
     $request->addKey("key1");
     $request->setForCas(true);
     $response = new MemcacheGetResponse();
     $item = $response->addItem();
     $item->setKey("key");
     $item->setValue("value");
     $item->setFlags(2);
     // Python's picked type.
     $item->setCasId(123456);
     $item = $response->addItem();
     $item->setKey("key1");
     $item->setValue("value1");
     $item->setFlags(0);
     // String.
     $item->setCasId(789);
     $this->apiProxyMock->expectCall('memcache', 'Get', $request, $response);
     $memcached = new Memcached();
     $keys = ["key", "key1"];
     $result = $memcached->getMulti($keys, $cas_tokens, Memcached::GET_PRESERVE_ORDER);
     $this->assertTrue(array_key_exists("key", $result));
     $this->assertNull($result["key"]);
     $this->assertTrue(array_key_exists("key", $cas_tokens));
     $this->assertNull($cas_tokens["key"]);
     $this->assertEquals("value1", $result["key1"]);
     $this->assertEquals(789, $cas_tokens["key1"]);
     $this->assertEquals($memcached->getResultCode(), Memcached::RES_SUCCESS);
     $this->apiProxyMock->verify();
 }
Ejemplo n.º 30
0
 /**
  * 获取指定键名序列的数据
  *
  * @param array $keys
  * @return array
  */
 public function getMulti($keys)
 {
     return $this->memcached ? $this->memcached->getMulti($keys) : null;
 }