public function set($url, &$item) { if (empty($url)) { throw new SagException('You need to provide a URL to cache.'); } if (!parent::mayCache($item)) { return false; } // If it already exists, then remove the old version but keep a copy if (isset($this->cache[$url])) { $oldCopy = json_decode($this->cache[$url]); self::remove($url); } $this->cache[$url] = json_encode($item); return isset($oldCopy) && is_object($oldCopy) ? $oldCopy : true; }
public function set($url, &$item) { if (empty($url)) { throw new SagException('You need to provide a URL to cache.'); } if (!parent::mayCache($item)) { return false; } // If it already exists, then remove the old version but keep a copy if ($this->cache[$url]) { $oldCopy = json_decode($this->cache[$url]); self::remove($url); } $serialized = $this->cache[$url] = $prevSize = $itemSize = 1; //for more accurate math $prevSize = memory_get_usage(); $serialized = json_encode($item); $itemSize = memory_get_usage() - $prevSize; self::addToSize($itemSize); $this->cache[$url] = $serialized; return isset($oldCopy) && is_object($oldCopy) ? $oldCopy : true; }
public function set($url, &$item) { if (empty($url)) { throw new SagException('You need to provide a URL to cache.'); } if (!parent::mayCache($item)) { return false; } $serialized = json_encode($item); $target = self::makeFilename($url); // If it already exists, then remove the old version but keep a copy if (is_file($target)) { $oldCopy = self::get($url); self::remove($url); } $fh = fopen($target, "w"); //in case self::remove() didn't get it? fwrite($fh, $serialized, strlen($serialized)); //don't throw up if we fail - we're not mission critical self::addToSize(filesize($target)); fclose($fh); // Only return the $oldCopy if it exists return isset($oldCopy) && is_object($oldCopy) ? $oldCopy : true; }