refactor: dont create multiple instances of the cache (#3504)

This commit is contained in:
Dag 2023-07-08 17:03:12 +02:00 committed by GitHub
parent b594ad2de3
commit c1c8304fc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 42 deletions

View file

@ -87,9 +87,7 @@ class DisplayAction implements ActionInterface
) )
); );
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope(''); $cache->setScope('');
$cache->setKey($cache_params); $cache->setKey($cache_params);
// This cache purge will basically delete all cache items older than 24h, regardless of scope and key // 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([ $cache->saveData([
'items' => array_map(function (FeedItem $item) { 'items' => array_map(function (FeedItem $item) {
return $item->toArray(); return $item->toArray();
@ -212,8 +213,7 @@ class DisplayAction implements ActionInterface
private static function logBridgeError($bridgeName, $code) private static function logBridgeError($bridgeName, $code)
{ {
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope('error_reporting'); $cache->setScope('error_reporting');
$cache->setkey([$bridgeName . '_' . $code]); $cache->setkey([$bridgeName . '_' . $code]);

View file

@ -35,9 +35,7 @@ class SetBridgeCacheAction implements ActionInterface
$bridge->loadConfiguration(); $bridge->loadConfiguration();
$value = $request['value']; $value = $request['value'];
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope(get_class($bridge)); $cache->setScope(get_class($bridge));
if (!is_array($key)) { if (!is_array($key)) {
// not sure if $key is an array when it comes in from request // not sure if $key is an array when it comes in from request

View file

@ -113,9 +113,7 @@ class ElloBridge extends BridgeAbstract
private function getAPIKey() private function getAPIKey()
{ {
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope('ElloBridge'); $cache->setScope('ElloBridge');
$cache->setKey(['key']); $cache->setKey(['key']);
$key = $cache->loadData(); $key = $cache->loadData();

View file

@ -98,9 +98,7 @@ class InstagramBridge extends BridgeAbstract
return $username; return $username;
} }
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope('InstagramBridge'); $cache->setScope('InstagramBridge');
$cache->setKey([$username]); $cache->setKey([$username]);
$key = $cache->loadData(); $key = $cache->loadData();

View file

@ -122,9 +122,7 @@ HTML;
return; return;
} }
$cacheFactory = new CacheFactory(); $this->clientIDCache = RssBridge::getCache();
$this->clientIDCache = $cacheFactory->create();
$this->clientIDCache->setScope('SoundCloudBridge'); $this->clientIDCache->setScope('SoundCloudBridge');
$this->clientIDCache->setKey(['client_id']); $this->clientIDCache->setKey(['client_id']);
} }

View file

@ -190,9 +190,7 @@ class SpotifyBridge extends BridgeAbstract
private function getToken() private function getToken()
{ {
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope('SpotifyBridge'); $cache->setScope('SpotifyBridge');
$cacheKey = sprintf('%s:%s', $this->getInput('clientid'), $this->getInput('clientsecret')); $cacheKey = sprintf('%s:%s', $this->getInput('clientid'), $this->getInput('clientsecret'));

View file

@ -223,9 +223,7 @@ EOD
// Try to get all tweets // Try to get all tweets
switch ($this->queriedContext) { switch ($this->queriedContext) {
case 'By username': case 'By username':
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope('twitter'); $cache->setScope('twitter');
$cache->setKey(['cache']); $cache->setKey(['cache']);
// todo: inspect mtime instead of purging with 3h // todo: inspect mtime instead of purging with 3h
@ -512,9 +510,7 @@ EOD;
//This function takes 2 requests, and therefore is cached //This function takes 2 requests, and therefore is cached
private function getApiKey($forceNew = 0) private function getApiKey($forceNew = 0)
{ {
$cacheFactory = new CacheFactory(); $r_cache = RssBridge::getCache();
$r_cache = $cacheFactory->create();
$scope = 'TwitterBridge'; $scope = 'TwitterBridge';
$r_cache->setScope($scope); $r_cache->setScope($scope);
$r_cache->setKey(['refresh']); $r_cache->setKey(['refresh']);
@ -530,7 +526,7 @@ EOD;
$cacheFactory = new CacheFactory(); $cacheFactory = new CacheFactory();
$cache = $cacheFactory->create(); $cache = RssBridge::getCache();
$cache->setScope($scope); $cache->setScope($scope);
$cache->setKey(['api_key']); $cache->setKey(['api_key']);
$data = $cache->loadData(); $data = $cache->loadData();
@ -565,9 +561,7 @@ EOD;
$apiKey = $data; $apiKey = $data;
} }
$cacheFac2 = new CacheFactory(); $gt_cache = RssBridge::getCache();
$gt_cache = $cacheFactory->create();
$gt_cache->setScope($scope); $gt_cache->setScope($scope);
$gt_cache->setKey(['guest_token']); $gt_cache->setKey(['guest_token']);
$guestTokenUses = $gt_cache->loadData(); $guestTokenUses = $gt_cache->loadData();

View file

@ -415,9 +415,7 @@ abstract class BridgeAbstract implements BridgeInterface
*/ */
protected function loadCacheValue(string $key, $duration = null) protected function loadCacheValue(string $key, $duration = null)
{ {
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
// Create class name without the namespace part // Create class name without the namespace part
$scope = $this->getShortName(); $scope = $this->getShortName();
$cache->setScope($scope); $cache->setScope($scope);
@ -441,9 +439,7 @@ abstract class BridgeAbstract implements BridgeInterface
*/ */
protected function saveCacheValue(string $key, $value) protected function saveCacheValue(string $key, $value)
{ {
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$scope = $this->getShortName(); $scope = $this->getShortName();
$cache->setScope($scope); $cache->setScope($scope);
$cache->setKey([$key]); $cache->setKey([$key]);

View file

@ -2,6 +2,8 @@
final class RssBridge final class RssBridge
{ {
private static CacheInterface $cache;
public function main(array $argv = []) public function main(array $argv = [])
{ {
if ($argv) { if ($argv) {
@ -69,6 +71,10 @@ final class RssBridge
// Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED); // Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
date_default_timezone_set(Configuration::getConfig('system', 'timezone')); date_default_timezone_set(Configuration::getConfig('system', 'timezone'));
// Create cache
$cacheFactory = new CacheFactory();
self::setCache($cacheFactory->create());
if (Configuration::getConfig('authentication', 'enable')) { if (Configuration::getConfig('authentication', 'enable')) {
$authenticationMiddleware = new AuthenticationMiddleware(); $authenticationMiddleware = new AuthenticationMiddleware();
$authenticationMiddleware(); $authenticationMiddleware();
@ -98,4 +104,14 @@ final class RssBridge
$response->send(); $response->send();
} }
} }
public static function getCache(): CacheInterface
{
return self::$cache;
}
public static function setCache(CacheInterface $cache): void
{
self::$cache = $cache;
}
} }

View file

@ -99,9 +99,7 @@ function getContents(
array $curlOptions = [], array $curlOptions = [],
bool $returnFull = false bool $returnFull = false
) { ) {
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope('server'); $cache->setScope('server');
$cache->setKey([$url]); $cache->setKey([$url]);
@ -419,9 +417,7 @@ function getSimpleHTMLDOMCached(
$defaultBRText = DEFAULT_BR_TEXT, $defaultBRText = DEFAULT_BR_TEXT,
$defaultSpanText = DEFAULT_SPAN_TEXT $defaultSpanText = DEFAULT_SPAN_TEXT
) { ) {
$cacheFactory = new CacheFactory(); $cache = RssBridge::getCache();
$cache = $cacheFactory->create();
$cache->setScope('pages'); $cache->setScope('pages');
$cache->setKey([$url]); $cache->setKey([$url]);