/** * Writes item into the cache. * @param string key * @param mixed data * @param array dependencies * @return void */ public function write($key, $data, array $dp) { if (isset($dp[Cache::ITEMS])) { throw new Nette\NotSupportedException('Dependent items are not supported by MemcachedStorage.'); } $key = $this->prefix . $key; $meta = array(self::META_DATA => $data); $expire = 0; if (isset($dp[Cache::EXPIRATION])) { $expire = (int) $dp[Cache::EXPIRATION]; if (!empty($dp[Cache::SLIDING])) { $meta[self::META_DELTA] = $expire; // sliding time } } if (isset($dp[Cache::CALLBACKS])) { $meta[self::META_CALLBACKS] = $dp[Cache::CALLBACKS]; } if (isset($dp[Cache::TAGS]) || isset($dp[Cache::PRIORITY])) { if (!$this->journal) { throw new Nette\InvalidStateException('CacheJournal has not been provided.'); } $this->journal->write($key, $dp); } $this->memcache->set($key, $meta, 0, $expire); }
/** * Writes item into the cache. * * @param string $key * @param mixed $data * @param array $dp * * @throws \Nette\InvalidStateException * @return void */ public function write($key, $data, array $dp) { $meta = [self::META_TIME => microtime()]; if (isset($dp[Cache::EXPIRATION])) { if (empty($dp[Cache::SLIDING])) { $meta[self::META_EXPIRE] = $dp[Cache::EXPIRATION] + time(); // absolute time } else { $meta[self::META_DELTA] = (int) $dp[Cache::EXPIRATION]; // sliding time } } if (isset($dp[Cache::ITEMS])) { foreach ((array) $dp[Cache::ITEMS] as $itemName) { $m = $this->readMeta($itemName); $meta[self::META_ITEMS][$itemName] = $m[self::META_TIME]; // may be NULL unset($m); } } if (isset($dp[Cache::CALLBACKS])) { $meta[self::META_CALLBACKS] = $dp[Cache::CALLBACKS]; } $cacheKey = $this->formatEntryKey($key); if (isset($dp[Cache::TAGS]) || isset($dp[Cache::PRIORITY])) { if (!$this->journal) { throw new Nette\InvalidStateException('CacheJournal has not been provided.'); } $this->journal->write($cacheKey, $dp); } if (!is_string($data) || $data === NULL) { $data = serialize($data); $meta[self::META_SERIALIZED] = TRUE; } $store = json_encode($meta) . Cache::NAMESPACE_SEPARATOR . $data; try { if (isset($dp[Cache::EXPIRATION])) { $this->client->send('setEX', [$cacheKey, $dp[Cache::EXPIRATION], $store]); } else { $this->client->send('set', [$cacheKey, $store]); } $this->unlock($key); } catch (RedisClientException $e) { $this->remove($key); throw new Nette\InvalidStateException($e->getMessage(), $e->getCode(), $e); } }
/** * Writes item into the cache. * @param string key * @param mixed data * @param array dependencies * @return void */ public function write($key, $data, array $dp) { $meta = [self::META_TIME => microtime()]; if (isset($dp[Cache::EXPIRATION])) { if (empty($dp[Cache::SLIDING])) { $meta[self::META_EXPIRE] = $dp[Cache::EXPIRATION] + time(); // absolute time } else { $meta[self::META_DELTA] = (int) $dp[Cache::EXPIRATION]; // sliding time } } if (isset($dp[Cache::ITEMS])) { foreach ((array) $dp[Cache::ITEMS] as $item) { $depFile = $this->getCacheFile($item); $m = $this->readMetaAndLock($depFile, LOCK_SH); $meta[self::META_ITEMS][$depFile] = $m[self::META_TIME]; // may be NULL unset($m); } } if (isset($dp[Cache::CALLBACKS])) { $meta[self::META_CALLBACKS] = $dp[Cache::CALLBACKS]; } if (!isset($this->locks[$key])) { $this->lock($key); if (!isset($this->locks[$key])) { return; } } $handle = $this->locks[$key]; unset($this->locks[$key]); $cacheFile = $this->getCacheFile($key); if (isset($dp[Cache::TAGS]) || isset($dp[Cache::PRIORITY])) { if (!$this->journal) { throw new Nette\InvalidStateException('CacheJournal has not been provided.'); } $this->journal->write($cacheFile, $dp); } ftruncate($handle, 0); if (!is_string($data)) { $data = serialize($data); $meta[self::META_SERIALIZED] = TRUE; } $head = serialize($meta) . '?>'; $head = '<?php //netteCache[01]' . str_pad((string) strlen($head), 6, '0', STR_PAD_LEFT) . $head; $headLen = strlen($head); do { if (fwrite($handle, str_repeat("", $headLen)) !== $headLen) { break; } if (fwrite($handle, $data) !== strlen($data)) { break; } fseek($handle, 0); if (fwrite($handle, $head) !== $headLen) { break; } flock($handle, LOCK_UN); fclose($handle); return; } while (FALSE); $this->delete($cacheFile, $handle); }