From 5cc12730c574b0af2e1957aa8e66afdecd0e1df5 Mon Sep 17 00:00:00 2001 From: Claus-Justus Heine Date: Mon, 30 Jun 2025 11:55:18 +0200 Subject: [PATCH] Cache: use at least a capped memory cache if a distributed cache is not available --- lib/Cache.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/Cache.php b/lib/Cache.php index f70cc61..7026e4a 100644 --- a/lib/Cache.php +++ b/lib/Cache.php @@ -5,6 +5,9 @@ * @copyright 2018 Marcin Łojewski * @author Marcin Łojewski * + * @copyright 2025 Claus-Justus Heine + * @author Claus-Justus Heine + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the @@ -39,7 +42,7 @@ class Cache /** * @var ICache The cache instance. */ - private $cache; + private $cache = null; /** * The default constructor. Initiates the cache memory. @@ -57,16 +60,20 @@ class Cache if ($useCache === App::FALSE_VALUE) { $this->cache = new NullCache(); - } elseif ($factory->isAvailable()) { - $this->cache = $factory->createDistributed(); - $logger->debug("Distributed cache initiated.", ["app" => $AppName]); } else { - $logger->debug( - "There's no distributed cache available, fallback to null cache.", - ["app" => $AppName] - ); - $this->cache = new NullCache(); - } + if ($factory->isAvailable()) { + $this->cache = $factory->createDistributed(); + } + if ($this->cache === null || ($this->cache instanceof NullCache)) { + $logger->debug( + "There's no distributed cache available, fallback to capped in-memory cache.", + ["app" => $AppName] + ); + $this->cache = $factory->createInMemory(128); + } else { + $logger->debug("Distributed cache initiated.", ["app" => $AppName]); + } + } } /**