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 From aedd62e2b84b4ea0d3c03f5c23ec594f4ebb1c17 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Thu, 13 Aug 2015 21:39:51 +0200 Subject: Cache: simplify cached content cleanup, improve tests Signed-off-by: VirtualTam --- application/Cache.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'application') diff --git a/application/Cache.php b/application/Cache.php index 9c7e818f..5d050165 100644 --- a/application/Cache.php +++ b/application/Cache.php @@ -7,26 +7,18 @@ * Purges all cached pages * * @param string $pageCacheDir page cache directory + * + * @return mixed an error string if the directory is missing */ function purgeCachedPages($pageCacheDir) { if (! is_dir($pageCacheDir)) { - return; + $error = 'Cannot purge '.$pageCacheDir.': no directory'; + error_log($error); + return $error; } - // TODO: check write access to the cache directory - - $handler = opendir($pageCacheDir); - if ($handler == false) { - return; - } - - while (($filename = readdir($handler)) !== false) { - if (endsWith($filename, '.cache')) { - unlink($pageCacheDir.'/'.$filename); - } - } - closedir($handler); + array_map('unlink', glob($pageCacheDir.'/*.cache')); } /** -- cgit v1.2.3