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