Skip to content

Commit

Permalink
perf(workflowengine): Cache query that is performed on every request
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Feb 6, 2023
1 parent 3473b69 commit 9b8dffa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 18 additions & 1 deletion apps/workflowengine/lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Storage\IStorage;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
Expand Down Expand Up @@ -120,6 +121,7 @@ class Manager implements IManager {

/** @var IConfig */
private $config;
private ICacheFactory $cacheFactory;

public function __construct(
IDBConnection $connection,
Expand All @@ -129,7 +131,8 @@ public function __construct(
ILogger $logger,
IUserSession $session,
IEventDispatcher $dispatcher,
IConfig $config
IConfig $config,
ICacheFactory $cacheFactory,
) {
$this->connection = $connection;
$this->container = $container;
Expand All @@ -140,6 +143,7 @@ public function __construct(
$this->session = $session;
$this->dispatcher = $dispatcher;
$this->config = $config;
$this->cacheFactory = $cacheFactory;
}

public function getRuleMatcher(): IRuleMatcher {
Expand All @@ -153,6 +157,12 @@ public function getRuleMatcher(): IRuleMatcher {
}

public function getAllConfiguredEvents() {
$cache = $this->cacheFactory->createDistributed('flow');
$cached = $cache->get('events');
if ($cached !== null) {
return $cached;
}

$query = $this->connection->getQueryBuilder();

$query->select('class', 'entity')
Expand All @@ -176,6 +186,8 @@ public function getAllConfiguredEvents() {
}
$result->closeCursor();

$cache->set('events', $operations, 3600);

return $operations;
}

Expand Down Expand Up @@ -289,6 +301,8 @@ protected function insertOperation(
]);
$query->execute();

$this->cacheFactory->createDistributed('flow')->remove('events');

return $query->getLastInsertId();
}

Expand Down Expand Up @@ -407,6 +421,7 @@ public function updateOperation(
throw $e;
}
unset($this->operations[$scopeContext->getHash()]);
$this->cacheFactory->createDistributed('flow')->remove('events');

return $this->getOperation($id);
}
Expand Down Expand Up @@ -444,6 +459,8 @@ public function deleteOperation($id, ScopeContext $scopeContext) {
unset($this->operations[$scopeContext->getHash()]);
}

$this->cacheFactory->createDistributed('flow')->remove('events');

return $result;
}

Expand Down
7 changes: 6 additions & 1 deletion apps/workflowengine/tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCA\WorkflowEngine\Manager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
Expand Down Expand Up @@ -76,6 +77,8 @@ class ManagerTest extends TestCase {
protected $dispatcher;
/** @var MockObject|IConfig */
protected $config;
/** @var MockObject|ICacheFactory */
protected $cacheFactory;

protected function setUp(): void {
parent::setUp();
Expand All @@ -94,6 +97,7 @@ protected function setUp(): void {
$this->session = $this->createMock(IUserSession::class);
$this->dispatcher = $this->createMock(IEventDispatcher::class);
$this->config = $this->createMock(IConfig::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);

$this->manager = new Manager(
\OC::$server->getDatabaseConnection(),
Expand All @@ -103,7 +107,8 @@ protected function setUp(): void {
$this->logger,
$this->session,
$this->dispatcher,
$this->config
$this->config,
$this->cacheFactory
);
$this->clearTables();
}
Expand Down

0 comments on commit 9b8dffa

Please sign in to comment.