From c1c8304fc05ffa2a2966434ec8251975100bf85b Mon Sep 17 00:00:00 2001
From: Dag <me@dvikan.no>
Date: Sat, 8 Jul 2023 17:03:12 +0200
Subject: [PATCH] refactor: dont create multiple instances of the cache (#3504)

---
 actions/DisplayAction.php        | 10 +++++-----
 actions/SetBridgeCacheAction.php |  4 +---
 bridges/ElloBridge.php           |  4 +---
 bridges/InstagramBridge.php      |  4 +---
 bridges/SoundcloudBridge.php     |  4 +---
 bridges/SpotifyBridge.php        |  4 +---
 bridges/TwitterBridge.php        | 14 ++++----------
 lib/BridgeAbstract.php           |  8 ++------
 lib/RssBridge.php                | 16 ++++++++++++++++
 lib/contents.php                 |  8 ++------
 10 files changed, 34 insertions(+), 42 deletions(-)

diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php
index ba87586b..cc910b45 100644
--- a/actions/DisplayAction.php
+++ b/actions/DisplayAction.php
@@ -87,9 +87,7 @@ class DisplayAction implements ActionInterface
             )
         );
 
-        $cacheFactory = new CacheFactory();
-
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         $cache->setScope('');
         $cache->setKey($cache_params);
         // This cache purge will basically delete all cache items older than 24h, regardless of scope and key
@@ -166,6 +164,9 @@ class DisplayAction implements ActionInterface
                 }
             }
 
+            // Unfortunately need to set scope and key again because they might be modified
+            $cache->setScope('');
+            $cache->setKey($cache_params);
             $cache->saveData([
                 'items' => array_map(function (FeedItem $item) {
                     return $item->toArray();
@@ -212,8 +213,7 @@ class DisplayAction implements ActionInterface
 
     private static function logBridgeError($bridgeName, $code)
     {
-        $cacheFactory = new CacheFactory();
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         $cache->setScope('error_reporting');
         $cache->setkey([$bridgeName . '_' . $code]);
 
diff --git a/actions/SetBridgeCacheAction.php b/actions/SetBridgeCacheAction.php
index 2f60fbc4..a9a598bd 100644
--- a/actions/SetBridgeCacheAction.php
+++ b/actions/SetBridgeCacheAction.php
@@ -35,9 +35,7 @@ class SetBridgeCacheAction implements ActionInterface
         $bridge->loadConfiguration();
         $value = $request['value'];
 
-        $cacheFactory = new CacheFactory();
-
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         $cache->setScope(get_class($bridge));
         if (!is_array($key)) {
             // not sure if $key is an array when it comes in from request
diff --git a/bridges/ElloBridge.php b/bridges/ElloBridge.php
index c45e554a..4cc1858b 100644
--- a/bridges/ElloBridge.php
+++ b/bridges/ElloBridge.php
@@ -113,9 +113,7 @@ class ElloBridge extends BridgeAbstract
 
     private function getAPIKey()
     {
-        $cacheFactory = new CacheFactory();
-
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         $cache->setScope('ElloBridge');
         $cache->setKey(['key']);
         $key = $cache->loadData();
diff --git a/bridges/InstagramBridge.php b/bridges/InstagramBridge.php
index 1bfa2472..71431906 100644
--- a/bridges/InstagramBridge.php
+++ b/bridges/InstagramBridge.php
@@ -98,9 +98,7 @@ class InstagramBridge extends BridgeAbstract
             return $username;
         }
 
-        $cacheFactory = new CacheFactory();
-
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         $cache->setScope('InstagramBridge');
         $cache->setKey([$username]);
         $key = $cache->loadData();
diff --git a/bridges/SoundcloudBridge.php b/bridges/SoundcloudBridge.php
index 09fc7b21..c9f55e9e 100644
--- a/bridges/SoundcloudBridge.php
+++ b/bridges/SoundcloudBridge.php
@@ -122,9 +122,7 @@ HTML;
             return;
         }
 
-        $cacheFactory = new CacheFactory();
-
-        $this->clientIDCache = $cacheFactory->create();
+        $this->clientIDCache = RssBridge::getCache();
         $this->clientIDCache->setScope('SoundCloudBridge');
         $this->clientIDCache->setKey(['client_id']);
     }
diff --git a/bridges/SpotifyBridge.php b/bridges/SpotifyBridge.php
index 48d225a7..170f4c86 100644
--- a/bridges/SpotifyBridge.php
+++ b/bridges/SpotifyBridge.php
@@ -190,9 +190,7 @@ class SpotifyBridge extends BridgeAbstract
 
     private function getToken()
     {
-        $cacheFactory = new CacheFactory();
-
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         $cache->setScope('SpotifyBridge');
 
         $cacheKey = sprintf('%s:%s', $this->getInput('clientid'), $this->getInput('clientsecret'));
diff --git a/bridges/TwitterBridge.php b/bridges/TwitterBridge.php
index 381dad76..befb8064 100644
--- a/bridges/TwitterBridge.php
+++ b/bridges/TwitterBridge.php
@@ -223,9 +223,7 @@ EOD
         // Try to get all tweets
         switch ($this->queriedContext) {
             case 'By username':
-                $cacheFactory = new CacheFactory();
-                $cache = $cacheFactory->create();
-
+                $cache = RssBridge::getCache();
                 $cache->setScope('twitter');
                 $cache->setKey(['cache']);
                 // todo: inspect mtime instead of purging with 3h
@@ -512,9 +510,7 @@ EOD;
     //This function takes 2 requests, and therefore is cached
     private function getApiKey($forceNew = 0)
     {
-        $cacheFactory = new CacheFactory();
-
-        $r_cache = $cacheFactory->create();
+        $r_cache = RssBridge::getCache();
         $scope = 'TwitterBridge';
         $r_cache->setScope($scope);
         $r_cache->setKey(['refresh']);
@@ -530,7 +526,7 @@ EOD;
 
         $cacheFactory = new CacheFactory();
 
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         $cache->setScope($scope);
         $cache->setKey(['api_key']);
         $data = $cache->loadData();
@@ -565,9 +561,7 @@ EOD;
             $apiKey = $data;
         }
 
-        $cacheFac2 = new CacheFactory();
-
-        $gt_cache = $cacheFactory->create();
+        $gt_cache = RssBridge::getCache();
         $gt_cache->setScope($scope);
         $gt_cache->setKey(['guest_token']);
         $guestTokenUses = $gt_cache->loadData();
diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php
index 21206a7b..3a2c47f2 100644
--- a/lib/BridgeAbstract.php
+++ b/lib/BridgeAbstract.php
@@ -415,9 +415,7 @@ abstract class BridgeAbstract implements BridgeInterface
      */
     protected function loadCacheValue(string $key, $duration = null)
     {
-        $cacheFactory = new CacheFactory();
-
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         // Create class name without the namespace part
         $scope = $this->getShortName();
         $cache->setScope($scope);
@@ -441,9 +439,7 @@ abstract class BridgeAbstract implements BridgeInterface
      */
     protected function saveCacheValue(string $key, $value)
     {
-        $cacheFactory = new CacheFactory();
-
-        $cache = $cacheFactory->create();
+        $cache = RssBridge::getCache();
         $scope = $this->getShortName();
         $cache->setScope($scope);
         $cache->setKey([$key]);
diff --git a/lib/RssBridge.php b/lib/RssBridge.php
index 1610e48d..79d1d710 100644
--- a/lib/RssBridge.php
+++ b/lib/RssBridge.php
@@ -2,6 +2,8 @@
 
 final class RssBridge
 {
+    private static CacheInterface $cache;
+
     public function main(array $argv = [])
     {
         if ($argv) {
@@ -69,6 +71,10 @@ final class RssBridge
         // Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
         date_default_timezone_set(Configuration::getConfig('system', 'timezone'));
 
+        // Create cache
+        $cacheFactory = new CacheFactory();
+        self::setCache($cacheFactory->create());
+
         if (Configuration::getConfig('authentication', 'enable')) {
             $authenticationMiddleware = new AuthenticationMiddleware();
             $authenticationMiddleware();
@@ -98,4 +104,14 @@ final class RssBridge
             $response->send();
         }
     }
+
+    public static function getCache(): CacheInterface
+    {
+        return self::$cache;
+    }
+
+    public static function setCache(CacheInterface $cache): void
+    {
+        self::$cache = $cache;
+    }
 }
diff --git a/lib/contents.php b/lib/contents.php
index b54bc9fe..67ad2d3f 100644
--- a/lib/contents.php
+++ b/lib/contents.php
@@ -99,9 +99,7 @@ function getContents(
     array $curlOptions = [],
     bool $returnFull = false
 ) {
-    $cacheFactory = new CacheFactory();
-
-    $cache = $cacheFactory->create();
+    $cache = RssBridge::getCache();
     $cache->setScope('server');
     $cache->setKey([$url]);
 
@@ -419,9 +417,7 @@ function getSimpleHTMLDOMCached(
     $defaultBRText = DEFAULT_BR_TEXT,
     $defaultSpanText = DEFAULT_SPAN_TEXT
 ) {
-    $cacheFactory = new CacheFactory();
-
-    $cache = $cacheFactory->create();
+    $cache = RssBridge::getCache();
     $cache->setScope('pages');
     $cache->setKey([$url]);