From 321ec7c8c16211192ca6ef7515c8880832adbd22 Mon Sep 17 00:00:00 2001 From: Dag Date: Tue, 5 Jul 2022 13:20:01 +0200 Subject: [PATCH] refactor: move cache logic into the factory (#2884) --- actions/DisplayAction.php | 2 +- bridges/ElloBridge.php | 2 +- bridges/InstagramBridge.php | 2 +- bridges/SoundcloudBridge.php | 2 +- bridges/SpotifyBridge.php | 2 +- bridges/TwitterBridge.php | 6 +++--- lib/BridgeAbstract.php | 4 ++-- lib/CacheFactory.php | 5 +++-- lib/contents.php | 4 ++-- lib/error.php | 2 +- 10 files changed, 16 insertions(+), 15 deletions(-) diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php index 721e9446..c404dcf5 100644 --- a/actions/DisplayAction.php +++ b/actions/DisplayAction.php @@ -101,7 +101,7 @@ class DisplayAction implements ActionInterface // Initialize cache $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope(''); $cache->purgeCache(86400); // 24 hours $cache->setKey($cache_params); diff --git a/bridges/ElloBridge.php b/bridges/ElloBridge.php index b0c7b09f..22830b49 100644 --- a/bridges/ElloBridge.php +++ b/bridges/ElloBridge.php @@ -115,7 +115,7 @@ class ElloBridge extends BridgeAbstract { $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope(get_called_class()); $cache->setKey(['key']); $key = $cache->loadData(); diff --git a/bridges/InstagramBridge.php b/bridges/InstagramBridge.php index ce7ff2bf..6b65bbcf 100644 --- a/bridges/InstagramBridge.php +++ b/bridges/InstagramBridge.php @@ -96,7 +96,7 @@ class InstagramBridge extends BridgeAbstract $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope(get_called_class()); $cache->setKey([$username]); $key = $cache->loadData(); diff --git a/bridges/SoundcloudBridge.php b/bridges/SoundcloudBridge.php index b2fce61d..4b77ea30 100644 --- a/bridges/SoundcloudBridge.php +++ b/bridges/SoundcloudBridge.php @@ -124,7 +124,7 @@ HTML; $cacheFac = new CacheFactory(); - $this->clientIDCache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $this->clientIDCache = $cacheFac->create(); $this->clientIDCache->setScope(get_called_class()); $this->clientIDCache->setKey(['client_id']); } diff --git a/bridges/SpotifyBridge.php b/bridges/SpotifyBridge.php index 312eddc6..48506e6d 100644 --- a/bridges/SpotifyBridge.php +++ b/bridges/SpotifyBridge.php @@ -102,7 +102,7 @@ class SpotifyBridge extends BridgeAbstract { $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope(get_called_class()); $cache->setKey(['token']); diff --git a/bridges/TwitterBridge.php b/bridges/TwitterBridge.php index 743843da..b4622788 100644 --- a/bridges/TwitterBridge.php +++ b/bridges/TwitterBridge.php @@ -505,7 +505,7 @@ EOD; { $cacheFac = new CacheFactory(); - $r_cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $r_cache = $cacheFac->create(); $r_cache->setScope(get_called_class()); $r_cache->setKey(['refresh']); $data = $r_cache->loadData(); @@ -520,7 +520,7 @@ EOD; $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope(get_called_class()); $cache->setKey(['api_key']); $data = $cache->loadData(); @@ -557,7 +557,7 @@ EOD; $cacheFac2 = new CacheFactory(); - $gt_cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $gt_cache = $cacheFac->create(); $gt_cache->setScope(get_called_class()); $gt_cache->setKey(['guest_token']); $guestTokenUses = $gt_cache->loadData(); diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index c479f53e..de6cd906 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -407,7 +407,7 @@ abstract class BridgeAbstract implements BridgeInterface { $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope(get_called_class()); $cache->setKey($key); if ($cache->getTime() < time() - $duration) { @@ -426,7 +426,7 @@ abstract class BridgeAbstract implements BridgeInterface { $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope(get_called_class()); $cache->setKey($key); $cache->saveData($value); diff --git a/lib/CacheFactory.php b/lib/CacheFactory.php index ba1c3cb9..0d0426a6 100644 --- a/lib/CacheFactory.php +++ b/lib/CacheFactory.php @@ -29,10 +29,11 @@ class CacheFactory } /** - * @param string $name The name of the cache e.g. "File", "Memcached" or "SQLite" + * @param string|null $name The name of the cache e.g. "File", "Memcached" or "SQLite" */ - public function create(string $name): CacheInterface + public function create(string $name = null): CacheInterface { + $name ??= Configuration::getConfig('cache', 'type'); $name = $this->sanitizeCacheName($name) . 'Cache'; if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) { diff --git a/lib/contents.php b/lib/contents.php index a01d81e1..aaa078d6 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -70,7 +70,7 @@ function getContents( ) { $cacheFactory = new CacheFactory(); - $cache = $cacheFactory->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFactory->create(); $cache->setScope('server'); $cache->purgeCache(86400); // 24 hours (forced) $cache->setKey([$url]); @@ -319,7 +319,7 @@ function getSimpleHTMLDOMCached( // Initialize cache $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope('pages'); $cache->purgeCache(86400); // 24 hours (forced) diff --git a/lib/error.php b/lib/error.php index f9950cea..39dd99f6 100644 --- a/lib/error.php +++ b/lib/error.php @@ -58,7 +58,7 @@ function logBridgeError($bridgeName, $code) { $cacheFac = new CacheFactory(); - $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); + $cache = $cacheFac->create(); $cache->setScope('error_reporting'); $cache->setkey($bridgeName . '_' . $code); $cache->purgeCache(86400); // 24 hours