/** * * @param string $key * @param mixed $value * @param array $options * @return bool */ public function store($key, $value, $options = array()) { if (!$this->online()) { return Gdn_Cache::CACHEOP_FAILURE; } $finalOptions = array_merge($this->StoreDefaults, $options); $expiry = (int) val(Gdn_Cache::FEATURE_EXPIRY, $finalOptions, 0); $useLocal = (bool) $finalOptions[Gdn_Cache::FEATURE_LOCAL]; $realKey = $this->makeKey($key, $finalOptions); // Should auto sharding be enabled? $keySize = strlen(serialize($value)); if ($this->hasFeature(Gdn_Cache::FEATURE_SHARD) && $keySize > Gdn_Cache::CACHE_SHARD_AUTO_SIZE) { $finalOptions[Gdn_Cache::FEATURE_SHARD] = true; } // Sharding, write real keys and manifest if (array_key_exists(Gdn_Cache::FEATURE_SHARD, $finalOptions) && ($shards = $finalOptions[Gdn_Cache::FEATURE_SHARD])) { $manifest = $this->shard($realKey, $value, $keySize, $shards); $shards = $manifest->shards; unset($manifest->shards); // Attempt to write manifest $added = $this->memcache->set($realKey, $manifest, $expiry); // Check if things went ok $ok = $this->lastAction($realKey); if (!$ok || !$added) { return Gdn_Cache::CACHEOP_FAILURE; } // Write real keys foreach ($shards as $shard) { $this->memcache->setByKey($shard['server'], $shard['key'], $shard['data'], $expiry); } unset($shards); if ($useLocal) { $this->localSet($realKey, $value); } return Gdn_Cache::CACHEOP_SUCCESS; } // Unsharded, write key $stored = $this->memcache->set($realKey, $value, $expiry); // Check if things went ok $ok = $this->lastAction($realKey); if (!$ok) { return Gdn_Cache::CACHEOP_FAILURE; } if ($stored) { if ($useLocal) { $this->localSet($realKey, $value); } return Gdn_Cache::CACHEOP_SUCCESS; } return Gdn_Cache::CACHEOP_FAILURE; }
/** * Sets a value in cache. * * The value is set whether or not this key already exists in memcached. * * @link http://www.php.net/manual/en/memcached.set.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 set($key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = 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)) { $this->add_to_internal_cache($derived_key, $value); return true; } // Save to Memcached if (false !== $byKey) { $result = $this->mc->setByKey($server_key, $derived_key, $value, $expiration); } else { $result = $this->mc->set($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; }
public function setValueByKey($key, $value) { return parent::setByKey($this->server, $key, $value, $this->experation); }
/** * @inheritdoc */ public function setByKey($server_key, $key, $value, $expiration = null) { $key = $this->prefix . $key; return parent::setByKey($server_key, $key, $value, $expiration); }