Skip to content

Commit

Permalink
feat(cache): get/set collabora response from local cache
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed Aug 15, 2024
1 parent 2079d79 commit 0d9e360
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
use OCP\Files\Template\Field;
use OCP\Files\Template\FieldType;
use OCP\Http\Client\IClientService;
use OCP\ICache;
use OCP\ICacheFactory;
use Psr\Log\LoggerInterface;

class TemplateFieldService {
private ICache $cache;

public function __construct(
private IClientService $clientService,
private CapabilitiesService $capabilitiesService,
Expand Down Expand Up @@ -47,6 +50,13 @@ public function extractFields(Node|int $file) {
throw new NotFoundException();
}

$cacheName = $file->getId() . "/" . $file->getEtag();
$cachedResponse = $this->cache->get($cacheName);

if ($cachedResponse) {
return $cachedResponse;
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$httpClient = $this->clientService->newClient();

Expand Down Expand Up @@ -84,7 +94,10 @@ public function extractFields(Node|int $file) {
];
}

return array_merge([], ...$fields);
$fields = array_merge([], ...$fields);
$this->cache->set($cacheName, $fields, 3600);

return $fields;
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return [];
Expand Down

0 comments on commit 0d9e360

Please sign in to comment.