本文整理汇总了PHP中Memcached::setMulti方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcached::setMulti方法的具体用法?PHP Memcached::setMulti怎么用?PHP Memcached::setMulti使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Memcached
的用法示例。
在下文中一共展示了Memcached::setMulti方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: putMany
/**
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param int $minutes
* @return void
*/
public function putMany(array $values, $minutes)
{
$prefixedValues = [];
foreach ($values as $key => $value) {
$prefixedValues[$this->prefix . $key] = $value;
}
$this->memcached->setMulti($prefixedValues, $minutes * 60);
}
示例2: setMulti
public function setMulti(array $items, $ttl = null)
{
$ttl = $ttl ?: 0;
if ($ttl > 0) {
$ttl = time() + $ttl;
}
return $this->memcached->setMulti($items, $ttl);
}
示例3: setMulti
/**
* @inheritdoc
*/
public function setMulti($values, $expire = 0, array $tags = [])
{
foreach ($values as $key => $value) {
$key = $this->prepareKey($key);
$this->setTags($key, $tags, $value);
$values[$key] = $this->serialize($value);
}
$this->storage->setMulti($values, $expire);
}
示例4: setMulti
/**
* @param array $data
* @param int $seconds
* @param bool $strict
* @throws CacheSetException
*/
public function setMulti($data, $seconds, $strict = true)
{
$set = $this->memcached->setMulti($data, $seconds);
if (!$set && $strict) {
throw new CacheSetException('multi');
}
}
示例5: setMulti
/**
* Set multiple values to cache at once.
*
* By sending an array of $items to this function, all values are saved at once to
* memcached, reducing the need for multiple requests to memcached. The $items array
* keys and values are what are stored to memcached. The keys in the $items array
* are merged with the $groups array/string value via buildKeys to determine the
* final key for the object.
*
* @link http://www.php.net/manual/en/memcached.setmulti.php
*
* @param array $items An array of key/value pairs to store on the server.
* @param string|array $groups Group(s) to merge with key(s) in $items.
* @param int $expiration The expiration time, defaults to 0.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $byKey True to store in internal cache by key; false to not store by key
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function setMulti($items, $groups = 'default', $expiration = 0, $server_key = '', $byKey = false)
{
// Build final keys and replace $items keys with the new keys
$derived_keys = $this->buildKeys(array_keys($items), $groups);
$expiration = $this->sanitize_expiration($expiration);
$derived_items = array_combine($derived_keys, $items);
$group_offset = empty($this->cache_key_salt) ? 1 : 2;
// Do not add to memcached if in no_mc_groups
foreach ($derived_items as $derived_key => $value) {
// Get the individual item's group
$key_pieces = explode(':', $derived_key);
// If group is a non-Memcached group, save to runtime cache, not Memcached
if (in_array($key_pieces[$group_offset], $this->no_mc_groups)) {
$this->add_to_internal_cache($derived_key, $value);
unset($derived_items[$derived_key]);
}
}
// Save to memcached
if (false !== $byKey) {
$result = $this->mc->setMultiByKey($server_key, $derived_items, $expiration);
} else {
$result = $this->mc->setMulti($derived_items, $expiration);
}
// Store in runtime cache if add was successful
if (Memcached::RES_SUCCESS === $this->getResultCode()) {
$this->cache = array_merge($this->cache, $derived_items);
}
return $result;
}
示例6: deleteMulti
/**
* {@inheritdoc}
*/
public function deleteMulti(array $keys)
{
if (!method_exists($this->client, 'deleteMulti')) {
/*
* HHVM doesn't support deleteMulti, so I'll hack around it by
* setting all items expired.
* I could also delete() all items, but that would probably take
* more network requests (this version always takes 2)
*
* @see http://docs.hhvm.com/manual/en/memcached.deletemulti.php
*/
$values = $this->getMulti($keys);
$this->client->setMulti(array_fill_keys(array_keys($values), ''), time() - 1);
$return = array();
foreach ($keys as $key) {
$return[$key] = array_key_exists($key, $values);
}
return $return;
}
$result = (array) $this->client->deleteMulti($keys);
/*
* Contrary to docs (http://php.net/manual/en/memcached.deletemulti.php)
* deleteMulti returns an array of [key => true] (for successfully
* deleted values) and [key => error code] (for failures)
* Pretty good because I want an array of true/false, so I'll just have
* to replace the error codes by falses.
*/
foreach ($result as $key => $status) {
$result[$key] = $status === true;
}
return $result;
}
示例7: setItems
/**
* Store multiple items.
*
* Options:
* - namespace <string> optional
* - The namespace to use (Default: namespace of object)
* - tags <array> optional
* - An array of tags
*
* @param array $keyValuePairs
* @param array $options
* @return boolean
* @throws Exception
*
* @triggers setItems.pre(PreEvent)
* @triggers setItems.post(PostEvent)
* @triggers setItems.exception(ExceptionEvent)
*/
public function setItems(array $keyValuePairs, array $options = array())
{
$baseOptions = $this->getOptions();
if (!$baseOptions->getWritable()) {
return false;
}
$this->normalizeOptions($options);
$args = new ArrayObject(array('keyValuePairs' => &$keyValuePairs, 'options' => &$options));
try {
$eventRs = $this->triggerPre(__FUNCTION__, $args);
if ($eventRs->stopped()) {
return $eventRs->last();
}
$internalKeyValuePairs = array();
$prefix = $options['namespace'] . $baseOptions->getNamespaceSeparator();
foreach ($keyValuePairs as $key => &$value) {
$internalKey = $prefix . $key;
$internalKeyValuePairs[$internalKey] =& $value;
}
$errKeys = $this->memcached->setMulti($internalKeyValuePairs, $options['ttl']);
if ($errKeys == false) {
throw new Exception\RuntimeException("Memcached::setMulti(<array>, {$options['ttl']}) failed");
}
$result = true;
return $this->triggerPost(__FUNCTION__, $args, $result);
} catch (\Exception $e) {
return $this->triggerException(__FUNCTION__, $args, $e);
}
}
示例8: internalSetItems
/**
* Internal method to store multiple items.
*
* Options:
* - ttl <float>
* - The time-to-life
* - namespace <string>
* - The namespace to use
*
* @param array $normalizedKeyValuePairs
* @param array $normalizedOptions
* @return boolean
* @throws Exception\ExceptionInterface
*/
protected function internalSetItems(array &$normalizedKeyValuePairs, array &$normalizedOptions)
{
$this->memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $normalizedOptions['namespace']);
$expiration = $this->expirationTime($normalizedOptions['ttl']);
if (!$this->memcached->setMulti($normalizedKeyValuePairs, $expiration)) {
throw $this->getExceptionByResultCode($this->memcached->getResultCode());
}
return true;
}
示例9: setMulti
/**
*
* @param array $items
* @param integer $time
* @return boolean
*/
public function setMulti(array $items, $time = 0)
{
// $_test = ($this->_baseKey !== false and is_array($items));
// if ($_test) {
// $this->_translateKeys($items);
// }
$_ret = $this->_memcached->setMulti($items, $time);
return $_ret;
}
示例10: _saveMulti
/**
* @param array $vos
* @return boolean
*/
protected function _saveMulti(array $vos = array())
{
if (!$this->memcached) {
return true;
}
$toSave = array();
foreach ($vos as $vo) {
$toSave[$this->getMemcachedCompleteKey($vo->getId())] = $vo;
}
return $this->memcached->setMulti($toSave);
}
示例11: set_many
/**
* Sets many items in the cache in a single transaction.
*
* @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
* keys, 'key' and 'value'.
* @return int The number of items successfully set. It is up to the developer to check this matches the number of items
* sent ... if they care that is.
*/
public function set_many(array $keyvaluearray)
{
$pairs = array();
foreach ($keyvaluearray as $pair) {
$pairs[$pair['key']] = $pair['value'];
}
if ($this->connection->setMulti($pairs, $this->definition->get_ttl())) {
return count($keyvaluearray);
}
return 0;
}
示例12: writeMany
/**
* Write many cache entries to the cache at once
*
* @param array $data An array of data to be stored in the cache
* @return array of bools for each key provided, true if the data was
* successfully cached, false on failure
*/
public function writeMany($data)
{
$cacheData = [];
foreach ($data as $key => $value) {
$cacheData[$this->_key($key)] = $value;
}
$success = $this->_Memcached->setMulti($cacheData);
$return = [];
foreach (array_keys($data) as $key) {
$return[$key] = $success;
}
return $return;
}
示例13: mSet
/**
* 批量设置键值
* @param array $sets 键值数组
* @return boolean 是否成功
*/
public function mSet($sets)
{
try {
foreach ($sets as &$value) {
$value = self::setValue($value);
}
return $this->handler->setMulti($sets);
} catch (Exception $ex) {
self::exception($ex);
//连接状态置为false
$this->isConnected = false;
}
return false;
}
示例14: storeValuesImpl
protected function storeValuesImpl(array $values, $expirationTime) {
$errorCacheEntryNames = NULL;
$adjustedExpirationTime = $expirationTime;
if (!isset($adjustedExpirationTime)) {
$adjustedExpirationTime = 0;
}
$storableValues = $deletableCacheEntryNames = NULL;
foreach ($values as $cacheEntryName => $value) {
if (isset($value)) {
$storableValues[$cacheEntryName] = $value;
}
else {
$deletableCacheEntryNames[] = $cacheEntryName;
}
}
if (isset($deletableCacheEntryNames)) {
foreach ($deletableCacheEntryNames as $deletableCacheEntryName) {
$result = $this->memcached->delete($deletableCacheEntryName);
if (($result === FALSE) && ($this->memcached->getResultCode() != Memcached::RES_NOTFOUND)) {
$errorCacheEntryNames[] = $deletableCacheEntryName;
LogHelper::log_error(t(
'[@cacheType] Internal error during value deletion: @message',
array('@cacheType' => self::CACHE__TYPE, '@message' => $this->memcached->getResultMessage())));
}
}
}
if (isset($storableValues)) {
$result = $this->memcached->setMulti($storableValues, $adjustedExpirationTime);
if ($result === FALSE) {
LogHelper::log_error(t(
'[@cacheType] Internal error during value storing: @message',
array('@cacheType' => self::CACHE__TYPE, '@message' => $this->memcached->getResultMessage())));
ArrayHelper::appendValue($errorCacheEntryNames, array_keys($storableValues));
}
}
return $errorCacheEntryNames;
}
示例15: set_many
/**
* Sets many items in the cache in a single transaction.
*
* @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
* keys, 'key' and 'value'.
* @return int The number of items successfully set. It is up to the developer to check this matches the number of items
* sent ... if they care that is.
*/
public function set_many(array $keyvaluearray)
{
$pairs = array();
foreach ($keyvaluearray as $pair) {
$pairs[$pair['key']] = $pair['value'];
}
$status = true;
if ($this->clustered) {
foreach ($this->setconnections as $connection) {
$status = $connection->setMulti($pairs, $this->definition->get_ttl()) && $status;
}
} else {
$status = $this->connection->setMulti($pairs, $this->definition->get_ttl());
}
if ($status) {
return count($keyvaluearray);
}
return 0;
}