]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/CachedPage.php
SessionManager+LoginManager: fix checkLoginState logic
[github/shaarli/Shaarli.git] / application / CachedPage.php
1 <?php
2 /**
3 * Simple cache system, mainly for the RSS/ATOM feeds
4 */
5 class CachedPage
6 {
7 // Directory containing page caches
8 private $cacheDir;
9
10 // Should this URL be cached (boolean)?
11 private $shouldBeCached;
12
13 // Name of the cache file for this URL
14 private $filename;
15
16 /**
17 * Creates a new CachedPage
18 *
19 * @param string $cacheDir page cache directory
20 * @param string $url page URL
21 * @param bool $shouldBeCached whether this page needs to be cached
22 */
23 public function __construct($cacheDir, $url, $shouldBeCached)
24 {
25 // TODO: check write access to the cache directory
26 $this->cacheDir = $cacheDir;
27 $this->filename = $this->cacheDir.'/'.sha1($url).'.cache';
28 $this->shouldBeCached = $shouldBeCached;
29 }
30
31 /**
32 * Returns the cached version of a page, if it exists and should be cached
33 *
34 * @return string a cached version of the page if it exists, null otherwise
35 */
36 public function cachedVersion()
37 {
38 if (!$this->shouldBeCached) {
39 return null;
40 }
41 if (is_file($this->filename)) {
42 return file_get_contents($this->filename);
43 }
44 return null;
45 }
46
47 /**
48 * Puts a page in the cache
49 *
50 * @param string $pageContent XML content to cache
51 */
52 public function cache($pageContent)
53 {
54 if (!$this->shouldBeCached) {
55 return;
56 }
57 file_put_contents($this->filename, $pageContent);
58 }
59 }