Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: decrease query count by cacheing some RDF property fields #3921

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use oat\tao\model\featureVisibility\FeatureVisibilityServiceProvider;
use oat\tao\model\import\ServiceProvider\ImportServiceProvider;
use oat\tao\model\LanguageServiceProvider;
use oat\tao\model\listener\PropertyServiceProvider;
use oat\tao\model\Lists\ServiceProvider\ListsServiceProvider;
use oat\tao\model\menu\MenuServiceProvider;
use oat\tao\model\metadata\ServiceProvider\MetadataServiceProvider;
Expand Down Expand Up @@ -413,6 +414,7 @@
ClientConfigServiceProvider::class,
HelperServiceProvider::class,
MenuServiceProvider::class,
PropertyServiceProvider::class,
],
'middlewares' => [
MiddlewareConfig::class,
Expand Down
55 changes: 55 additions & 0 deletions models/classes/listener/ClassPropertyCacheWarmupListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA;
*
*/

declare(strict_types=1);

namespace oat\tao\model\listener;

use ArrayIterator;
use helpers_PropertyCache;
use oat\generis\model\data\event\CacheWarmupEvent;
use oat\generis\model\data\Ontology;
use oat\oatbox\reporting\Report;
use oat\tao\scripts\update\OntologyUpdater;

class ClassPropertyCacheWarmupListener
{
private Ontology $model;

public function __construct(Ontology $model)
{
$this->model = $model;
}

public function handleEvent(CacheWarmupEvent $event): void
{
$existingTriples = OntologyUpdater::getCurrentTriples($this->model);

$properties = [];
foreach ($existingTriples as $triple) {
$properties[$triple->predicate][] = $triple->subject;
}

helpers_PropertyCache::warmupCachedValuesByProperties(array_keys($properties));

$event->addReport(Report::createInfo('Generated property cache.'));
}
}
55 changes: 55 additions & 0 deletions models/classes/listener/PropertyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\tao\model\listener;

use common_ext_ExtensionsManager;
use oat\generis\model\data\Ontology;
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\oatbox\cache\SimpleCache;
use oat\tao\model\ClientLibConfigRegistry;
use oat\tao\model\featureFlag\Listener\FeatureFlagCacheWarmupListener;
use oat\tao\model\featureFlag\Repository\FeatureFlagRepository;
use oat\tao\model\featureFlag\Repository\FeatureFlagRepositoryInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

/**
* @codeCoverageIgnore
*/
class PropertyServiceProvider implements ContainerServiceProviderInterface
{
public function __invoke(ContainerConfigurator $configurator): void
{
$services = $configurator->services();

$services
->set(ClassPropertyCacheWarmupListener::class, ClassPropertyCacheWarmupListener::class)
->public()
->args(
[
service(Ontology::SERVICE_ID)
]
);
}
}
7 changes: 6 additions & 1 deletion scripts/install/RegisterEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
namespace oat\tao\scripts\install;

use oat\generis\model\data\event\CacheWarmupEvent;
use oat\generis\model\OntologyAwareTrait;
use oat\oatbox\event\EventManager;
use oat\oatbox\extension\InstallAction;
use oat\generis\model\OntologyAwareTrait;
use oat\oatbox\reporting\Report;
use oat\tao\model\featureFlag\Listener\FeatureFlagCacheWarmupListener;
use oat\tao\model\Language\Listener\LanguageCacheWarmupListener;
use oat\tao\model\listener\ClassPropertyCacheWarmupListener;
use oat\tao\model\menu\Listener\MenuCacheWarmupListener;
use oat\tao\model\migrations\MigrationsService;
use oat\tao\model\routing\Listener\AnnotationCacheWarmupListener;
Expand Down Expand Up @@ -66,6 +67,10 @@ public function __invoke($params)
CacheWarmupEvent::class,
[MenuCacheWarmupListener::class, 'handleEvent']
);
$eventManager->attach(
CacheWarmupEvent::class,
[ClassPropertyCacheWarmupListener::class, 'handleEvent']
);
$this->getServiceManager()->register(EventManager::SERVICE_ID, $eventManager);

return Report::createSuccess('Events registered');
Expand Down
2 changes: 2 additions & 0 deletions scripts/update/OntologyUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use AppendIterator;
use common_ext_ExtensionsManager;
use helpers_RdfDiff;
use helpers_PropertyCache;
use oat\generis\model\data\Model;
use oat\generis\model\data\ModelManager;
use oat\generis\model\GenerisRdf;
Expand All @@ -45,6 +46,7 @@ public static function syncModels()
self::logDiff($diff);

$diff->applyTo($currentModel);
helpers_PropertyCache::clearCachedValuesByTriples($diff->getTriplesToRemove());
}

public static function correctModelId($rdfFile)
Expand Down