/** * Adds a value to cache. * * If the specified key already exists, the value is not stored and the function * returns false. * * @link http://www.php.net/manual/en/memcached.add.php * * @param string $key The key under which to store the value. * @param mixed $value The value to store. * @param string $group The group value appended to the $key. * @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 add($key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false) { /** * Ensuring that wp_suspend_cache_addition is defined before calling, because sometimes an advanced-cache.php * file will load object-cache.php before wp-includes/functions.php is loaded. In those cases, if wp_cache_add * is called in advanced-cache.php before any more of WordPress is loaded, we get a fatal error because * wp_suspend_cache_addition will not be defined until wp-includes/functions.php is loaded. */ if (function_exists('wp_suspend_cache_addition') && wp_suspend_cache_addition()) { return false; } $derived_key = $this->buildKey($key, $group); $expiration = $this->sanitize_expiration($expiration); // If group is a non-Memcached group, save to runtime cache, not Memcached if (in_array($group, $this->no_mc_groups)) { // Add does not set the value if the key exists; mimic that here if (isset($this->cache[$derived_key])) { return false; } $this->add_to_internal_cache($derived_key, $value); return true; } // Save to Memcached if (false !== $byKey) { $result = $this->mc->addByKey($server_key, $derived_key, $value, $expiration); } else { $result = $this->mc->add($derived_key, $value, $expiration); } // Store in runtime cache if add was successful if (Memcached::RES_SUCCESS === $this->getResultCode()) { $this->add_to_internal_cache($derived_key, $value); } return $result; }
/** * @inheritdoc */ public function addByKey($server_key, $key, $value, $expiration = null) { $key = $this->prefix . $key; return parent::addByKey($server_key, $key, $value, $expiration); }