Skip to content

Commit

Permalink
Discovery: Add a memory cache in CollaboraDiscovery for the parsed xml.
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Jan 17, 2025
1 parent b99927c commit 70760de
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
33 changes: 26 additions & 7 deletions src/Discovery/CollaboraDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@
namespace Drupal\collabora_online\Discovery;

use Drupal\collabora_online\Exception\CollaboraNotAvailableException;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Symfony\Component\ErrorHandler\ErrorHandler;

/**
* Service to get values from the discovery.xml.
*/
class CollaboraDiscovery implements CollaboraDiscoveryInterface {

/**
* Constructor.
*
* @param \Drupal\collabora_online\Discovery\CollaboraDiscoveryFetcherInterface $discoveryFetcher
* Service to load the discovery.xml from the Collabora server.
*/
protected const DEFAULT_CID = 'collabora_online.discovery.parsed';

public function __construct(
protected readonly CollaboraDiscoveryFetcherInterface $discoveryFetcher,
protected readonly MemoryCacheInterface $cache,
protected readonly TimeInterface $time,
protected readonly string $cid = self::DEFAULT_CID,
) {}

/**
Expand Down Expand Up @@ -75,9 +77,26 @@ public function getProofKeyOld(): ?string {
* Fetching the discovery.xml failed, or the result is not valid xml.
*/
protected function getParsedXml(): \SimpleXMLElement {
$cached = $this->cache->get($this->cid);
if ($cached) {
return $cached->data;
}
$cacheability = new CacheableMetadata();
$xml = $this->discoveryFetcher->getDiscoveryXml($cacheability);
return $this->parseXml($xml);
$parsed_xml = $this->parseXml($xml);

$max_age = $cacheability->getCacheMaxAge();
/* @see \Drupal\Core\Cache\VariationCache::maxAgeToExpire() */
$expire = ($max_age === Cache::PERMANENT)
? Cache::PERMANENT
: $max_age + $this->time->getRequestTime();
$this->cache->set(
$this->cid,
$parsed_xml,
$expire,
$cacheability->getCacheTags(),
);
return $parsed_xml;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/src/Unit/CollaboraDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Drupal\collabora_online\Discovery\CollaboraDiscoveryFetcherInterface;
use Drupal\collabora_online\Discovery\CollaboraDiscoveryInterface;
use Drupal\collabora_online\Exception\CollaboraNotAvailableException;
use Drupal\Component\Datetime\Time;
use Drupal\Core\Cache\MemoryCache\MemoryCache;
use Drupal\Tests\UnitTestCase;

/**
Expand Down Expand Up @@ -146,7 +148,12 @@ protected function getDiscoveryFromFile(string $file): CollaboraDiscoveryInterfa
protected function getDiscoveryFromXml(string $xml): CollaboraDiscoveryInterface {
$fetcher = $this->createMock(CollaboraDiscoveryFetcherInterface::class);
$fetcher->method('getDiscoveryXml')->willReturn($xml);
return new CollaboraDiscovery($fetcher);
$time = new Time();
return new CollaboraDiscovery(
$fetcher,
new MemoryCache($time),
$time,
);
}

}

0 comments on commit 70760de

Please sign in to comment.