aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCôme Chilliet <come.chilliet@nextcloud.com>2024-06-06 16:23:28 +0200
committerCôme Chilliet <91878298+come-nc@users.noreply.github.com>2024-06-11 14:10:29 +0200
commit98f3ea657c4ec7b1b0c15230fbbfbd06a978a6f0 (patch)
treea56e803fd82392cfdca35f50b395bbfd7f4833e8
parente111d2e26cbc50fb252c940980574a8579ababde (diff)
downloadnextcloud-server-98f3ea657c4ec7b1b0c15230fbbfbd06a978a6f0.tar.gz
nextcloud-server-98f3ea657c4ec7b1b0c15230fbbfbd06a978a6f0.zip
fix: Cache webhooks listened events for 5min
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
-rw-r--r--apps/webhooks/lib/AppInfo/Application.php35
-rw-r--r--apps/webhooks/lib/Db/WebhookListenerMapper.php1
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<string>
- * TODO cache
*/
public function getAllConfiguredEvents(): array {
$qb = $this->db->getQueryBuilder();