refactor: move cache logic into the factory (#2884)

This commit is contained in:
Dag 2022-07-05 13:20:01 +02:00 committed by GitHub
parent 5b9b579652
commit 321ec7c8c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 16 additions and 15 deletions

View File

@ -101,7 +101,7 @@ class DisplayAction implements ActionInterface
// Initialize cache // Initialize cache
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope(''); $cache->setScope('');
$cache->purgeCache(86400); // 24 hours $cache->purgeCache(86400); // 24 hours
$cache->setKey($cache_params); $cache->setKey($cache_params);

View File

@ -115,7 +115,7 @@ class ElloBridge extends BridgeAbstract
{ {
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope(get_called_class()); $cache->setScope(get_called_class());
$cache->setKey(['key']); $cache->setKey(['key']);
$key = $cache->loadData(); $key = $cache->loadData();

View File

@ -96,7 +96,7 @@ class InstagramBridge extends BridgeAbstract
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope(get_called_class()); $cache->setScope(get_called_class());
$cache->setKey([$username]); $cache->setKey([$username]);
$key = $cache->loadData(); $key = $cache->loadData();

View File

@ -124,7 +124,7 @@ HTML;
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$this->clientIDCache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $this->clientIDCache = $cacheFac->create();
$this->clientIDCache->setScope(get_called_class()); $this->clientIDCache->setScope(get_called_class());
$this->clientIDCache->setKey(['client_id']); $this->clientIDCache->setKey(['client_id']);
} }

View File

@ -102,7 +102,7 @@ class SpotifyBridge extends BridgeAbstract
{ {
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope(get_called_class()); $cache->setScope(get_called_class());
$cache->setKey(['token']); $cache->setKey(['token']);

View File

@ -505,7 +505,7 @@ EOD;
{ {
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$r_cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $r_cache = $cacheFac->create();
$r_cache->setScope(get_called_class()); $r_cache->setScope(get_called_class());
$r_cache->setKey(['refresh']); $r_cache->setKey(['refresh']);
$data = $r_cache->loadData(); $data = $r_cache->loadData();
@ -520,7 +520,7 @@ EOD;
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope(get_called_class()); $cache->setScope(get_called_class());
$cache->setKey(['api_key']); $cache->setKey(['api_key']);
$data = $cache->loadData(); $data = $cache->loadData();
@ -557,7 +557,7 @@ EOD;
$cacheFac2 = new CacheFactory(); $cacheFac2 = new CacheFactory();
$gt_cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $gt_cache = $cacheFac->create();
$gt_cache->setScope(get_called_class()); $gt_cache->setScope(get_called_class());
$gt_cache->setKey(['guest_token']); $gt_cache->setKey(['guest_token']);
$guestTokenUses = $gt_cache->loadData(); $guestTokenUses = $gt_cache->loadData();

View File

@ -407,7 +407,7 @@ abstract class BridgeAbstract implements BridgeInterface
{ {
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope(get_called_class()); $cache->setScope(get_called_class());
$cache->setKey($key); $cache->setKey($key);
if ($cache->getTime() < time() - $duration) { if ($cache->getTime() < time() - $duration) {
@ -426,7 +426,7 @@ abstract class BridgeAbstract implements BridgeInterface
{ {
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope(get_called_class()); $cache->setScope(get_called_class());
$cache->setKey($key); $cache->setKey($key);
$cache->saveData($value); $cache->saveData($value);

View File

@ -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'; $name = $this->sanitizeCacheName($name) . 'Cache';
if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) { if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {

View File

@ -70,7 +70,7 @@ function getContents(
) { ) {
$cacheFactory = new CacheFactory(); $cacheFactory = new CacheFactory();
$cache = $cacheFactory->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFactory->create();
$cache->setScope('server'); $cache->setScope('server');
$cache->purgeCache(86400); // 24 hours (forced) $cache->purgeCache(86400); // 24 hours (forced)
$cache->setKey([$url]); $cache->setKey([$url]);
@ -319,7 +319,7 @@ function getSimpleHTMLDOMCached(
// Initialize cache // Initialize cache
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope('pages'); $cache->setScope('pages');
$cache->purgeCache(86400); // 24 hours (forced) $cache->purgeCache(86400); // 24 hours (forced)

View File

@ -58,7 +58,7 @@ function logBridgeError($bridgeName, $code)
{ {
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create();
$cache->setScope('error_reporting'); $cache->setScope('error_reporting');
$cache->setkey($bridgeName . '_' . $code); $cache->setkey($bridgeName . '_' . $code);
$cache->purgeCache(86400); // 24 hours $cache->purgeCache(86400); // 24 hours