diff options
Diffstat (limited to 'application')
-rw-r--r-- | application/Cache.php | 46 | ||||
-rw-r--r-- | application/CachedPage.php | 63 | ||||
-rw-r--r-- | application/LinkDB.php | 7 |
3 files changed, 113 insertions, 3 deletions
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 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Cache utilities | ||
4 | */ | ||
5 | |||
6 | /** | ||
7 | * Purges all cached pages | ||
8 | * | ||
9 | * @param string $pageCacheDir page cache directory | ||
10 | */ | ||
11 | function purgeCachedPages($pageCacheDir) | ||
12 | { | ||
13 | if (! is_dir($pageCacheDir)) { | ||
14 | return; | ||
15 | } | ||
16 | |||
17 | // TODO: check write access to the cache directory | ||
18 | |||
19 | $handler = opendir($pageCacheDir); | ||
20 | if ($handler == false) { | ||
21 | return; | ||
22 | } | ||
23 | |||
24 | while (($filename = readdir($handler)) !== false) { | ||
25 | if (endsWith($filename, '.cache')) { | ||
26 | unlink($pageCacheDir.'/'.$filename); | ||
27 | } | ||
28 | } | ||
29 | closedir($handler); | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Invalidates caches when the database is changed or the user logs out. | ||
34 | * | ||
35 | * @param string $pageCacheDir page cache directory | ||
36 | */ | ||
37 | function invalidateCaches($pageCacheDir) | ||
38 | { | ||
39 | // Purge cache attached to session. | ||
40 | if (isset($_SESSION['tags'])) { | ||
41 | unset($_SESSION['tags']); | ||
42 | } | ||
43 | |||
44 | // Purge page cache shared by sessions. | ||
45 | purgeCachedPages($pageCacheDir); | ||
46 | } | ||
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 | */ | ||
5 | class 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 | } | ||
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 | |||
269 | 269 | ||
270 | /** | 270 | /** |
271 | * Saves the database from memory to disk | 271 | * Saves the database from memory to disk |
272 | * | ||
273 | * @param string $pageCacheDir page cache directory | ||
272 | */ | 274 | */ |
273 | public function savedb() | 275 | public function savedb($pageCacheDir) |
274 | { | 276 | { |
275 | if (!$this->_loggedIn) { | 277 | if (!$this->_loggedIn) { |
276 | // TODO: raise an Exception instead | 278 | // TODO: raise an Exception instead |
@@ -280,7 +282,7 @@ You use the community supported version of the original Shaarli project, by Seba | |||
280 | $this->_datastore, | 282 | $this->_datastore, |
281 | self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix | 283 | self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix |
282 | ); | 284 | ); |
283 | invalidateCaches(); | 285 | invalidateCaches($pageCacheDir); |
284 | } | 286 | } |
285 | 287 | ||
286 | /** | 288 | /** |
@@ -439,4 +441,3 @@ You use the community supported version of the original Shaarli project, by Seba | |||
439 | return $linkDays; | 441 | return $linkDays; |
440 | } | 442 | } |
441 | } | 443 | } |
442 | ?> | ||