/**
  * 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;
 }
Ejemplo n.º 2
0
    }
    return true;
}
$data = array('foo' => 'foo-data', 'bar' => 'bar-data', 'baz' => 'baz-data', 'lol' => 'lol-data', 'kek' => 'kek-data');
$keys = array_keys($data);
$null = null;
$m->setMulti($data, 3600);
/* Check that all keys were stored */
var_dump(has_all_keys($keys, $m->getMulti($keys)));
/* Check that all keys get deleted */
$deleted = $m->deleteMulti($keys);
var_dump(has_all_keys($keys, $deleted, true));
/* Try to get the deleted keys, should give empty array */
var_dump($m->getMulti($keys));
/* ---- same tests for byKey variants ---- */
$m->setMultiByKey("hi", $data, 3600);
var_dump(has_all_keys($keys, $m->getMultiByKey('hi', $keys)));
/* Check that all keys get deleted */
$deleted = $m->deleteMultiByKey('hi', $keys);
var_dump(has_all_keys($keys, $deleted, true));
/* Try to get the deleted keys, should give empty array */
var_dump($m->getMultiByKey('hi', $keys));
/* Test deleting non-existent keys */
$keys = array();
$keys[] = "nothere";
$keys[] = "nothere2";
$retval = $m->deleteMulti($keys);
foreach ($retval as $key => $value) {
    if ($value === Memcached::RES_NOTFOUND) {
        echo "{$key} NOT FOUND\n";
    }
 /**
  * @inheritdoc
  */
 public function setMultiByKey($server_key, array $items, $expiration = null)
 {
     $items = $this->prefixArrayKeys($items);
     return parent::setMultiByKey($server_key, $items, $expiration);
 }