From d373b7b452c6d64b47180940ed88c99c21bd9bc3 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 19 Dec 2022 21:36:50 +0300 Subject: * bring back cache-busting for feed icons based on timestamp * DiskCache: use singleton pattern to create less cache object instances * DiskCache: implement ETag --- classes/diskcache.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'classes/diskcache.php') diff --git a/classes/diskcache.php b/classes/diskcache.php index bf7031587..96c826728 100644 --- a/classes/diskcache.php +++ b/classes/diskcache.php @@ -3,6 +3,8 @@ class DiskCache implements Cache_Adapter { /** @var Cache_Adapter $adapter */ private $adapter; + private static $instances = []; + /** * https://stackoverflow.com/a/53662733 * @@ -195,6 +197,13 @@ class DiskCache implements Cache_Adapter { 'text/x-scriptzsh' => 'zsh' ]; + public static function instance(string $dir) : DiskCache { + if ((self::$instances[$dir] ?? null) == null) + self::$instances[$dir] = new self($dir); + + return self::$instances[$dir]; + } + public function __construct(string $dir) { foreach (PluginHost::getInstance()->get_plugins() as $n => $p) { if (implements_interface($p, "Cache_Adapter")) { @@ -302,9 +311,10 @@ class DiskCache implements Cache_Adapter { return false; } - $gmt_modified = gmdate("D, d M Y H:i:s", (int)$this->get_mtime($filename)) . " GMT"; + $file_mtime = $this->get_mtime($filename); + $gmt_modified = gmdate("D, d M Y H:i:s", (int)$file_mtime) . " GMT"; - if (($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? '') == $gmt_modified) { + if (($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? '') == $gmt_modified || ($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') == $file_mtime) { header('HTTP/1.1 304 Not Modified'); return false; } @@ -339,7 +349,8 @@ class DiskCache implements Cache_Adapter { header("Expires: $stamp_expires", true); header("Last-Modified: $gmt_modified", true); - header("Cache-Control: public"); + header("Cache-Control: no-cache"); + header("ETag: $file_mtime"); header_remove("Pragma"); @@ -378,7 +389,7 @@ class DiskCache implements Cache_Adapter { $doc = new DOMDocument(); if (@$doc->loadHTML('' . $res)) { $xpath = new DOMXPath($doc); - $cache = new DiskCache("images"); + $cache = DiskCache::instance("images"); $entries = $xpath->query('(//img[@src]|//source[@src|@srcset]|//video[@poster|@src])'); -- cgit v1.2.3