From 98f3ea657c4ec7b1b0c15230fbbfbd06a978a6f0 Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Thu, 6 Jun 2024 16:23:28 +0200 Subject: fix: Cache webhooks listened events for 5min MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/webhooks/lib/AppInfo/Application.php | 35 +++++++++++++++++++++----- apps/webhooks/lib/Db/WebhookListenerMapper.php | 1 - 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/apps/webhooks/lib/AppInfo/Application.php b/apps/webhooks/lib/AppInfo/Application.php index a8836a25f54..cbbacd3ed48 100644 --- a/apps/webhooks/lib/AppInfo/Application.php +++ b/apps/webhooks/lib/AppInfo/Application.php @@ -16,14 +16,25 @@ use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\EventDispatcher\IEventDispatcher; +use OCP\ICache; +use OCP\ICacheFactory; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; class Application extends App implements IBootstrap { public const APP_ID = 'webhooks'; - public function __construct() { + private ?ICache $cache = null; + + private const CACHE_KEY = 'eventsUsedInWebhooks'; + + public function __construct( + ICacheFactory $cacheFactory, + ) { parent::__construct(self::APP_ID); + if ($cacheFactory->isAvailable()) { + $this->cache = $cacheFactory->createDistributed(); + } } public function register(IRegistrationContext $context): void { @@ -38,13 +49,10 @@ class Application extends App implements IBootstrap { ContainerInterface $container, LoggerInterface $logger, ): void { - /** @var WebhookListenerMapper */ - $mapper = $container->get(WebhookListenerMapper::class); - /* Listen to all events with at least one webhook configured */ - $configuredEvents = $mapper->getAllConfiguredEvents(); + $configuredEvents = $this->getAllConfiguredEvents($container); foreach ($configuredEvents as $eventName) { - // $logger->error($eventName.' '.\OCP\Files\Events\Node\NodeWrittenEvent::class, ['exception' => new \Exception('coucou')]); + $logger->debug("Listening to {$eventName}"); $dispatcher->addServiceListener( $eventName, WebhooksEventListener::class, @@ -52,4 +60,19 @@ class Application extends App implements IBootstrap { ); } } + + /** + * List all events with at least one webhook configured, with cache + */ + private function getAllConfiguredEvents(ContainerInterface $container) { + $events = $this->cache?->get(self::CACHE_KEY); + if ($events !== null) { + return json_decode($events); + } + /** @var WebhookListenerMapper */ + $mapper = $container->get(WebhookListenerMapper::class); + $events = $mapper->getAllConfiguredEvents(); + // cache for 5 minutes + $this->cache?->set(self::CACHE_KEY, json_encode($events), 300); + } } diff --git a/apps/webhooks/lib/Db/WebhookListenerMapper.php b/apps/webhooks/lib/Db/WebhookListenerMapper.php index 85c167b0c92..a8985ae6e88 100644 --- a/apps/webhooks/lib/Db/WebhookListenerMapper.php +++ b/apps/webhooks/lib/Db/WebhookListenerMapper.php @@ -138,7 +138,6 @@ class WebhookListenerMapper extends QBMapper { /** * @throws Exception * @return list - * TODO cache */ public function getAllConfiguredEvents(): array { $qb = $this->db->getQueryBuilder(); -- cgit v1.2.3