2020-01-23 21:13:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shaarli\Render;
|
|
|
|
|
2020-05-17 14:16:32 +02:00
|
|
|
use Shaarli\Feed\CachedPage;
|
|
|
|
|
2020-01-23 21:13:41 +01:00
|
|
|
/**
|
|
|
|
* Cache utilities
|
|
|
|
*/
|
|
|
|
class PageCacheManager
|
|
|
|
{
|
|
|
|
/** @var string Cache directory */
|
|
|
|
protected $pageCacheDir;
|
|
|
|
|
2020-05-17 14:16:32 +02:00
|
|
|
/** @var bool */
|
|
|
|
protected $isLoggedIn;
|
|
|
|
|
|
|
|
public function __construct(string $pageCacheDir, bool $isLoggedIn)
|
2020-01-23 21:13:41 +01:00
|
|
|
{
|
|
|
|
$this->pageCacheDir = $pageCacheDir;
|
2020-05-17 14:16:32 +02:00
|
|
|
$this->isLoggedIn = $isLoggedIn;
|
2020-01-23 21:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purges all cached pages
|
|
|
|
*
|
|
|
|
* @return string|null an error string if the directory is missing
|
|
|
|
*/
|
|
|
|
public function purgeCachedPages(): ?string
|
|
|
|
{
|
|
|
|
if (!is_dir($this->pageCacheDir)) {
|
|
|
|
$error = sprintf(t('Cannot purge %s: no directory'), $this->pageCacheDir);
|
|
|
|
error_log($error);
|
|
|
|
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_map('unlink', glob($this->pageCacheDir . '/*.cache'));
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates caches when the database is changed or the user logs out.
|
|
|
|
*/
|
|
|
|
public function invalidateCaches(): void
|
|
|
|
{
|
|
|
|
// Purge page cache shared by sessions.
|
|
|
|
$this->purgeCachedPages();
|
|
|
|
}
|
2020-05-17 14:16:32 +02:00
|
|
|
|
|
|
|
public function getCachePage(string $pageUrl): CachedPage
|
|
|
|
{
|
|
|
|
return new CachedPage(
|
|
|
|
$this->pageCacheDir,
|
|
|
|
$pageUrl,
|
|
|
|
false === $this->isLoggedIn
|
|
|
|
);
|
|
|
|
}
|
2020-01-23 21:13:41 +01:00
|
|
|
}
|