Example #1
0
 /**
  * Sets the value of a key in the cache regardless of whether it is currently
  * present or not.
  *
  * @param string $key The key associated with the value that will be replaced
  *                    in the cache.
  *
  * @param mixed $value The new cache value.
  *
  * @param int $flag This parameter is present only for compatibility and is
  *                  ignored.
  *
  * @param int $expire The delay before the item is removed from the cache. If
  *                    $expire <= 2592000 then it is interpreted as the number
  *                    of seconds from the time of the call to wait before
  *                    removing the item from the cache. If $expire > 2592000
  *                    then it is interpreted as the absolute Unix epoch time
  *                    when the value will expire.
  *
  * @return bool true if the item was successfully replaced the cache, false
  *              otherwise.
  */
 public function set($key, $value, $flag = null, $expire = 0)
 {
     // Sending of a key of 'null' or an unset value is a failure.
     if (is_null($key)) {
         return false;
     }
     try {
         $set_results = MemcacheUtils::setMultiWithPolicy(array($key => $value), $expire, SetPolicy::SET);
     } catch (Error $e) {
         return false;
     }
     return $set_results[0] == SetStatusCode::STORED;
 }
Example #2
0
 /**
  * Is similar to Memcached::set(), but instead of a single key/value item, it
  * works on multiple items specified in items.
  *
  * @see Memcached::set()
  *
  * @param array $items An array of key value pairs to set.
  * @param int $expiration The expiration time to set for the value.
  *
  * returns bool true if the call succeeds, false otherwise.
  */
 public function setMulti($items, $expiration = 0)
 {
     if (array_key_exists(self::OPT_PREFIX_KEY, $this->options)) {
         $new_items = array();
         foreach ($items as $key => $value) {
             $new_items[$this->getPrefixKey($key)] = $value;
         }
         $items = $new_items;
     }
     try {
         $set_results = MemcacheUtils::setMultiWithPolicy($items, $expiration, SetPolicy::SET);
     } catch (Exception $e) {
         $this->result_code = self::RES_FAILURE;
         return false;
     }
     // If any fail, report this method as failed.
     foreach ($set_results as $result) {
         if ($result != SetStatusCode::STORED) {
             $this->result_code = self::RES_NOTSTORED;
             return false;
         }
     }
     $this->result_code = self::RES_SUCCESS;
     return true;
 }