aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/CachedPage.php
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-08-13 23:54:26 +0200
committerVirtualTam <virtualtam@flibidi.net>2015-08-13 23:54:26 +0200
commita3b1b4ae709bd3c5a8ec6fe29aa36fd01c52bca5 (patch)
tree003ac85fc10d2b9b5051f299fda083a9ced69972 /application/CachedPage.php
parent5ac5349ac053b1e560b136c62f8c764fd3230039 (diff)
parentaedd62e2b84b4ea0d3c03f5c23ec594f4ebb1c17 (diff)
downloadShaarli-a3b1b4ae709bd3c5a8ec6fe29aa36fd01c52bca5.tar.gz
Shaarli-a3b1b4ae709bd3c5a8ec6fe29aa36fd01c52bca5.tar.zst
Shaarli-a3b1b4ae709bd3c5a8ec6fe29aa36fd01c52bca5.zip
Merge pull request #309 from virtualtam/refactor/PageCache
CachedPage: move to a proper file, add tests
Diffstat (limited to 'application/CachedPage.php')
-rw-r--r--application/CachedPage.php63
1 files changed, 63 insertions, 0 deletions
diff --git a/application/CachedPage.php b/application/CachedPage.php
new file mode 100644
index 00000000..50cfa9ac
--- /dev/null
+++ b/application/CachedPage.php
@@ -0,0 +1,63 @@
1<?php
2/**
3 * Simple cache system, mainly for the RSS/ATOM feeds
4 */
5class CachedPage
6{
7 // Directory containing page caches
8 private $cacheDir;
9
10 // Full URL of the page to cache -typically the value returned by pageUrl()
11 private $url;
12
13 // Should this URL be cached (boolean)?
14 private $shouldBeCached;
15
16 // Name of the cache file for this URL
17 private $filename;
18
19 /**
20 * Creates a new CachedPage
21 *
22 * @param string $cacheDir page cache directory
23 * @param string $url page URL
24 * @param bool $shouldBeCached whether this page needs to be cached
25 */
26 public function __construct($cacheDir, $url, $shouldBeCached)
27 {
28 // TODO: check write access to the cache directory
29 $this->cacheDir = $cacheDir;
30 $this->url = $url;
31 $this->filename = $this->cacheDir.'/'.sha1($url).'.cache';
32 $this->shouldBeCached = $shouldBeCached;
33 }
34
35 /**
36 * Returns the cached version of a page, if it exists and should be cached
37 *
38 * @return a cached version of the page if it exists, null otherwise
39 */
40 public function cachedVersion()
41 {
42 if (!$this->shouldBeCached) {
43 return null;
44 }
45 if (is_file($this->filename)) {
46 return file_get_contents($this->filename);
47 }
48 return null;
49 }
50
51 /**
52 * Puts a page in the cache
53 *
54 * @param string $pageContent XML content to cache
55 */
56 public function cache($pageContent)
57 {
58 if (!$this->shouldBeCached) {
59 return;
60 }
61 file_put_contents($this->filename, $pageContent);
62 }
63}