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 = $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]);

View file

@ -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

View file

@ -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();

View file

@ -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();

View file

@ -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']);
}

View file

@ -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'));

View file

@ -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();

View file

@ -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]);

View file

@ -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;
}
}

View file

@ -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]);