From 6b3d3f155698ac72e21f3046e1cd278439a98adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 6 Nov 2020 10:54:41 +0100 Subject: Store discovery response in distributed cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Command/ActivateConfig.php | 2 +- lib/Controller/SettingsController.php | 2 +- lib/WOPI/DiscoveryManager.php | 74 ++++++++++++----------------------- 3 files changed, 26 insertions(+), 52 deletions(-) (limited to 'lib') diff --git a/lib/Command/ActivateConfig.php b/lib/Command/ActivateConfig.php index 793d9fde..68413521 100644 --- a/lib/Command/ActivateConfig.php +++ b/lib/Command/ActivateConfig.php @@ -65,7 +65,7 @@ class ActivateConfig extends Command { protected function execute(InputInterface $input, OutputInterface $output) { try { - $this->discoveryManager->refretch(); + $this->discoveryManager->refetch(); $this->capabilitiesService->clear(); $capaUrlSrc = $this->wopiParser->getUrlSrc('Capabilities'); if (is_array($capaUrlSrc) && $capaUrlSrc['action'] === 'getinfo') { diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 2b649eaf..e5a45dd8 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -169,7 +169,7 @@ class SettingsController extends Controller{ $this->appConfig->setAppValue('canonical_webroot', $canonical_webroot); } - $this->discoveryManager->refretch(); + $this->discoveryManager->refetch(); $this->capabilitiesService->clear(); try { $capaUrlSrc = $this->wopiParser->getUrlSrc('Capabilities'); diff --git a/lib/WOPI/DiscoveryManager.php b/lib/WOPI/DiscoveryManager.php index 8c6075cc..585a0c76 100644 --- a/lib/WOPI/DiscoveryManager.php +++ b/lib/WOPI/DiscoveryManager.php @@ -21,70 +21,45 @@ namespace OCA\Richdocuments\WOPI; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\Files\IAppData; -use OCP\Files\NotFoundException; -use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Http\Client\IClientService; +use OCP\ICache; +use OCP\ICacheFactory; use OCP\IConfig; -use OCP\IL10N; class DiscoveryManager { + /** @var IClientService */ private $clientService; - /** @var ISimpleFolder */ - private $appData; + /** @var ICache */ + private $cache; /** @var IConfig */ private $config; - /** @var IL10N */ - private $l10n; - /** @var ITimeFactory */ - private $timeFactory; - /** - * @param IClientService $clientService - * @param IAppData $appData - * @param IConfig $config - * @param IL10N $l10n - * @param ITimeFactory $timeFactory - */ + /** @var string */ + private $discovery; + public function __construct(IClientService $clientService, - IAppData $appData, - IConfig $config, - IL10N $l10n, - ITimeFactory $timeFactory) { + ICacheFactory $cacheFactory, + IConfig $config) { $this->clientService = $clientService; - try { - $this->appData = $appData->getFolder('richdocuments'); - } catch (NotFoundException $e) { - $this->appData = $appData->newFolder('richdocuments'); - } + $this->cache = $cacheFactory->createDistributed('richdocuments'); $this->config = $config; - $this->timeFactory = $timeFactory; } public function get() { - // First check if there is a local valid discovery file - try { - $file = $this->appData->getFile('discovery.xml'); - $decodedFile = json_decode($file->getContent(), true); - if($decodedFile['timestamp'] + 3600 > $this->timeFactory->getTime()) { - return $decodedFile['data']; - } - } catch (NotFoundException $e) { - $file = $this->appData->newFile('discovery.xml'); + if ($this->discovery) { + return $this->discovery; } - $response = $this->fetchFromRemote(); + $this->discovery = $this->cache->get('discovery'); + if (!$this->discovery) { + $response = $this->fetchFromRemote(); + $responseBody = $response->getBody(); + $this->discovery = $responseBody; + $this->cache->set('discovery', $this->discovery, 3600); + } - $responseBody = $response->getBody(); - $file->putContent( - json_encode([ - 'data' => $responseBody, - 'timestamp' => $this->timeFactory->getTime(), - ]) - ); - return $responseBody; + return $this->discovery; } /** @@ -109,9 +84,8 @@ class DiscoveryManager { } } - public function refretch() { - try { - $this->appData->getFile('discovery.xml')->delete(); - } catch(\Exception $e) {} + public function refetch() { + $this->cache->remove('discovery'); + $this->discovery = null; } } -- cgit v1.2.3