/** * * @param string $fileName * @param string $contents * * @return int */ public function write(\Fwk\Cache\CacheEntry $entry) { $fileInfos = implode(DIRECTORY_SEPARATOR, array($this->cacheDirectory, md5('cacheKey:' . $entry->getKey()) . '.cache')); $fileContents = implode(DIRECTORY_SEPARATOR, array($this->cacheDirectory, md5('cacheKey:' . $entry->getKey()))); $entry->setSerializer($this->serializer); $serialized = $entry->getSerializedContents(); file_put_contents($fileInfos, $this->serializer->serialize($entry)); file_put_contents($fileContents, $serialized); return true; }
/** * Stores an item ($item) in the Cache at index $key. * * Lifetime ($maxAge) could be: * - An integer (seconds) * - Null (infinite lifetime) * - Relative (eg: 1day, 2hours, 4years, 45secs) * - A Closure that must return a boolean value telling if the entry * is expired or not (eg: md5 hash comparision). * * @param string $key Cache key name * @param mixed $item The item we want to store * @param mixed $maxAge The lifetime of the item * @param array $tags Optional tags for later retrieving * * @return CacheEntry * @throws Exceptions\WriteError if an error occurs */ public function put($key, $item, $maxAge = null, array $tags = array()) { $entry = new CacheEntry($item, $key); $entry->setMaxAge($maxAge); $entry->setTags($tags); $res = $this->adapter->write($entry); if (!$res) { throw new Exceptions\WriteError(); } return $entry; }
/** * * @param string $fileName * @param string $contents * * @return int */ public function write(\Fwk\Cache\CacheEntry $entry) { if ($this->exists($entry->getKey())) { $this->delete($entry->getKey()); } $maxAge = $entry->getMaxAge(); if (is_object($maxAge)) { $maxAge = $this->serializer->serialize($maxAge); } $query = "INSERT INTO %s VALUES (%s,%s,%s,%s)"; $stmt = $this->pdo->exec(sprintf($query, $this->options['table.infos'], $this->pdo->quote($entry->getKey()), $this->pdo->quote($entry->getCreatedOn()), $this->pdo->quote($maxAge), $this->pdo->quote(implode(self::TAGS_SEPARATOR, $entry->getTags())))); $entry->setSerializer($this->serializer); $query = "INSERT INTO %s VALUES (%s, %s)"; $stmt = $this->pdo->exec(sprintf($query, $this->options['table.entries'], $this->pdo->quote($entry->getKey()), $this->pdo->quote($entry->getSerializedContents()))); return true; }