1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-08-01 14:20:16 +02:00

refactor(cache): extract and encapsulate cache expiration logic (#3547)

* refactor(cache): extract and encapsulate cache expiration logic

* fix: logic bug in getSimpleHTMLDOMCached

* fix: silly me, index should of course be on the key column

* silly me again, PRIMARY keys get index by default lol

* comment out the delete portion in loadData

* remove a few log statements

* tweak twitter cache timeout
This commit is contained in:
Dag
2023-07-19 05:05:49 +02:00
committed by GitHub
parent 087e790ec1
commit 6254b8593e
12 changed files with 124 additions and 137 deletions

View File

@@ -409,26 +409,15 @@ abstract class BridgeAbstract implements BridgeInterface
/**
* Loads a cached value for the specified key
*
* @param int $duration Cache duration (optional)
* @param int $timeout Cache duration (optional)
* @return mixed Cached value or null if the key doesn't exist or has expired
*/
protected function loadCacheValue(string $key, $duration = null)
protected function loadCacheValue(string $key, int $timeout = 86400)
{
$cache = RssBridge::getCache();
// Create class name without the namespace part
$scope = $this->getShortName();
$cache->setScope($scope);
$cache->setScope($this->getShortName());
$cache->setKey([$key]);
$timestamp = $cache->getTime();
if (
$duration
&& $timestamp
&& $timestamp < time() - $duration
) {
return null;
}
return $cache->loadData();
return $cache->loadData($timeout);
}
/**
@@ -439,8 +428,7 @@ abstract class BridgeAbstract implements BridgeInterface
protected function saveCacheValue(string $key, $value)
{
$cache = RssBridge::getCache();
$scope = $this->getShortName();
$cache->setScope($scope);
$cache->setScope($this->getShortName());
$cache->setKey([$key]);
$cache->saveData($value);
}

View File

@@ -6,11 +6,11 @@ interface CacheInterface
public function setKey(array $key): void;
public function loadData();
public function loadData(int $timeout = 86400);
public function saveData($data): void;
public function getTime(): ?int;
public function purgeCache(int $seconds): void;
public function purgeCache(int $timeout = 86400): void;
}

View File

@@ -11,8 +11,13 @@ class TwitterClient
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
$this->authorization = 'AAAAAAAAAAAAAAAAAAAAAGHtAgAAAAAA%2Bx7ILXNILCqkSGIzy6faIHZ9s3Q%3DQy97w6SIrzE7lQwPJEYQBsArEE2fC25caFwRBvAGi456G09vGR';
$cache->setScope('twitter');
$cache->setKey(['cache']);
$cache->purgeCache(60 * 60 * 3);
$this->data = $this->cache->loadData() ?? [];
$this->authorization = 'AAAAAAAAAAAAAAAAAAAAAGHtAgAAAAAA%2Bx7ILXNILCqkSGIzy6faIHZ9s3Q%3DQy97w6SIrzE7lQwPJEYQBsArEE2fC25caFwRBvAGi456G09vGR';
}
public function fetchUserTweets(string $screenName): \stdClass
@@ -22,7 +27,6 @@ class TwitterClient
$userInfo = $this->fetchUserInfoByScreenName($screenName);
} catch (HttpException $e) {
if ($e->getCode() === 403) {
Logger::info('The guest token has expired');
$this->data['guest_token'] = null;
$this->fetchGuestToken();
$userInfo = $this->fetchUserInfoByScreenName($screenName);
@@ -35,7 +39,6 @@ class TwitterClient
$timeline = $this->fetchTimeline($userInfo->rest_id);
} catch (HttpException $e) {
if ($e->getCode() === 403) {
Logger::info('The guest token has expired');
$this->data['guest_token'] = null;
$this->fetchGuestToken();
$timeline = $this->fetchTimeline($userInfo->rest_id);
@@ -88,7 +91,6 @@ class TwitterClient
private function fetchGuestToken(): void
{
if (isset($this->data['guest_token'])) {
Logger::info('Reusing cached guest token: ' . $this->data['guest_token']);
return;
}
$url = 'https://api.twitter.com/1.1/guest/activate.json';
@@ -99,7 +101,6 @@ class TwitterClient
$this->cache->setScope('twitter');
$this->cache->setKey(['cache']);
$this->cache->saveData($this->data);
Logger::info("Fetch new guest token: $guest_token");
}
private function fetchUserInfoByScreenName(string $screenName)
@@ -115,7 +116,7 @@ class TwitterClient
'https://twitter.com/i/api/graphql/hc-pka9A7gyS3xODIafnrQ/UserByScreenName?variables=%s',
urlencode(json_encode($variables))
);
$response = json_decode(getContents($url, $this->createHttpHeaders()));
$response = Json::decode(getContents($url, $this->createHttpHeaders()), false);
if (isset($response->errors)) {
// Grab the first error message
throw new \Exception(sprintf('From twitter api: "%s"', $response->errors[0]->message));
@@ -168,7 +169,7 @@ class TwitterClient
urlencode(json_encode($variables)),
urlencode(json_encode($features))
);
$response = json_decode(getContents($url, $this->createHttpHeaders()));
$response = Json::decode(getContents($url, $this->createHttpHeaders()), false);
return $response;
}

View File

@@ -100,9 +100,6 @@ function getContents(
bool $returnFull = false
) {
$httpClient = RssBridge::getHttpClient();
$cache = RssBridge::getCache();
$cache->setScope('server');
$cache->setKey([$url]);
// Snagged from https://github.com/lwthiker/curl-impersonate/blob/main/firefox/curl_ff102
$defaultHttpHeaders = [
@@ -138,6 +135,11 @@ function getContents(
if (Configuration::getConfig('proxy', 'url') && !defined('NOPROXY')) {
$config['proxy'] = Configuration::getConfig('proxy', 'url');
}
$cache = RssBridge::getCache();
$cache->setScope('server');
$cache->setKey([$url]);
if (!Debug::isEnabled() && $cache->getTime()) {
$config['if_not_modified_since'] = $cache->getTime();
}
@@ -384,7 +386,7 @@ function getSimpleHTMLDOM(
* _Notice_: Cached contents are forcefully removed after 24 hours (86400 seconds).
*
* @param string $url The URL.
* @param int $duration Cache duration in seconds.
* @param int $timeout Cache duration in seconds.
* @param array $header (optional) A list of cURL header.
* For more information follow the links below.
* * https://php.net/manual/en/function.curl-setopt.php
@@ -409,7 +411,7 @@ function getSimpleHTMLDOM(
*/
function getSimpleHTMLDOMCached(
$url,
$duration = 86400,
$timeout = 86400,
$header = [],
$opts = [],
$lowercase = true,
@@ -422,29 +424,15 @@ function getSimpleHTMLDOMCached(
$cache = RssBridge::getCache();
$cache->setScope('pages');
$cache->setKey([$url]);
// Determine if cached file is within duration
$time = $cache->getTime();
if (
$time
&& time() - $duration < $time
&& !Debug::isEnabled()
) {
// Cache hit
$content = $cache->loadData();
} else {
$content = getContents(
$url,
$header ?? [],
$opts ?? []
);
if ($content) {
$cache->setScope('pages');
$cache->setKey([$url]);
$cache->saveData($content);
}
$content = $cache->loadData($timeout);
if (!$content || Debug::isEnabled()) {
$content = getContents($url, $header ?? [], $opts ?? []);
}
if ($content) {
$cache->setScope('pages');
$cache->setKey([$url]);
$cache->saveData($content);
}
return str_get_html(
$content,
$lowercase,