From 01e48f269df59e02798dad4a698c125d76b0ed70 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Thu, 9 Jul 2015 22:14:39 +0200 Subject: CachedPage: move to a proper file, add tests Modifications - rename `pageCache` to `CachedPage` - move utilities to `Cache` - do not access globals - apply coding rules - update LinkDB and test code - add test coverage Signed-off-by: VirtualTam --- application/Cache.php | 46 +++++++++++++++++++++++++++++++++ application/CachedPage.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++ application/LinkDB.php | 7 +++--- 3 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 application/Cache.php create mode 100644 application/CachedPage.php (limited to 'application') diff --git a/application/Cache.php b/application/Cache.php new file mode 100644 index 00000000..9c7e818f --- /dev/null +++ b/application/Cache.php @@ -0,0 +1,46 @@ +cacheDir = $cacheDir; + $this->url = $url; + $this->filename = $this->cacheDir.'/'.sha1($url).'.cache'; + $this->shouldBeCached = $shouldBeCached; + } + + /** + * Returns the cached version of a page, if it exists and should be cached + * + * @return a cached version of the page if it exists, null otherwise + */ + public function cachedVersion() + { + if (!$this->shouldBeCached) { + return null; + } + if (is_file($this->filename)) { + return file_get_contents($this->filename); + } + return null; + } + + /** + * Puts a page in the cache + * + * @param string $pageContent XML content to cache + */ + public function cache($pageContent) + { + if (!$this->shouldBeCached) { + return; + } + file_put_contents($this->filename, $pageContent); + } +} diff --git a/application/LinkDB.php b/application/LinkDB.php index 1e16fef1..463aa47e 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -269,8 +269,10 @@ You use the community supported version of the original Shaarli project, by Seba /** * Saves the database from memory to disk + * + * @param string $pageCacheDir page cache directory */ - public function savedb() + public function savedb($pageCacheDir) { if (!$this->_loggedIn) { // TODO: raise an Exception instead @@ -280,7 +282,7 @@ You use the community supported version of the original Shaarli project, by Seba $this->_datastore, self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix ); - invalidateCaches(); + invalidateCaches($pageCacheDir); } /** @@ -439,4 +441,3 @@ You use the community supported version of the original Shaarli project, by Seba return $linkDays; } } -?> -- cgit v1.2.3