From 00c87047412bf352a714a206ebea33ce61c3b3ea Mon Sep 17 00:00:00 2001 From: Bout Date: Tue, 12 May 2020 10:16:14 +0200 Subject: [PATCH 01/11] Add psr16 dependency --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6c03daa6a..7c47d7a58 100755 --- a/composer.json +++ b/composer.json @@ -54,6 +54,7 @@ "tao-extension-name": "generis" }, "require": { + "php": "^7.1", "clearfw/clearfw": "~1.2.0", "easyrdf/easyrdf": "^0.9.0", "doctrine/dbal": "2.10.1", @@ -61,13 +62,13 @@ "zendframework/zend-servicemanager": "~2.5.0", "league/flysystem": "~1.0", "oat-sa/oatbox-extension-installer": "~1.1||dev-master", - "php": "^7.1", - "psr/log": "~1.0", "oat-sa/lib-generis-search": "2.1.2", "monolog/monolog": "^1.23.0", "fluent/logger": "^1.0.1", "symfony/lock": "^3.4", + "psr/log": "~1.0", "psr/container": "^1.0.0", + "psr/simple-cache" : "^1.0.1", "ramsey/uuid": "^3.8" }, "require-dev": { From cc11077c2b886b3aa971e04861e1ee79d331ddf1 Mon Sep 17 00:00:00 2001 From: Bout Date: Tue, 12 May 2020 15:17:52 +0200 Subject: [PATCH 02/11] Fixed header use statement --- common/oatbox/user/LoginFailedException.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/common/oatbox/user/LoginFailedException.php b/common/oatbox/user/LoginFailedException.php index 1f195494a..a398a0707 100644 --- a/common/oatbox/user/LoginFailedException.php +++ b/common/oatbox/user/LoginFailedException.php @@ -1,8 +1,29 @@ Date: Tue, 12 May 2020 15:19:12 +0200 Subject: [PATCH 03/11] Add keyvalue mock trait --- common/persistence/class.InMemoryKvDriver.php | 1 + common/persistence/sql/interface.Driver.php | 5 ++- test/KeyValueMockTrait.php | 44 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/KeyValueMockTrait.php diff --git a/common/persistence/class.InMemoryKvDriver.php b/common/persistence/class.InMemoryKvDriver.php index 7d71f97a4..ae3c8f239 100644 --- a/common/persistence/class.InMemoryKvDriver.php +++ b/common/persistence/class.InMemoryKvDriver.php @@ -65,6 +65,7 @@ public function del($id) public function purge() { $this->persistence = []; + return true; } public function incr($id) diff --git a/common/persistence/sql/interface.Driver.php b/common/persistence/sql/interface.Driver.php index 6429afa43..c320a6b1d 100644 --- a/common/persistence/sql/interface.Driver.php +++ b/common/persistence/sql/interface.Driver.php @@ -51,7 +51,10 @@ public function insertMultiple($tableName, array $data); public function updateMultiple($tableName, array $data); public function getSchemaManager(); - + + /** + * @return common_persistence_sql_Platform + */ public function getPlatForm(); public function lastInsertId($name = null); diff --git a/test/KeyValueMockTrait.php b/test/KeyValueMockTrait.php new file mode 100644 index 000000000..f742b8689 --- /dev/null +++ b/test/KeyValueMockTrait.php @@ -0,0 +1,44 @@ +prophesize(PersistenceManager::class); + $pmProphecy->setServiceLocator(Argument::any())->willReturn(null); + $pmProphecy->getPersistenceById($key)->willReturn($persistence); + return $pmProphecy->reveal(); + } +} From 089108a98ac112b8326bc1fac80ded46bf90e89f Mon Sep 17 00:00:00 2001 From: Bout Date: Tue, 12 May 2020 15:23:18 +0200 Subject: [PATCH 04/11] Provide Psr16 cache --- common/oatbox/cache/KeyValueCache.php | 100 +++++++++++++++++ common/oatbox/cache/MultipleCacheTrait.php | 61 +++++++++++ common/oatbox/cache/SimpleCache.php | 29 +++++ config/default/SimpleCache.conf.php | 11 ++ test/unit/oatbox/cache/KeyValueCacheTest.php | 107 +++++++++++++++++++ 5 files changed, 308 insertions(+) create mode 100644 common/oatbox/cache/KeyValueCache.php create mode 100644 common/oatbox/cache/MultipleCacheTrait.php create mode 100644 common/oatbox/cache/SimpleCache.php create mode 100644 config/default/SimpleCache.conf.php create mode 100644 test/unit/oatbox/cache/KeyValueCacheTest.php diff --git a/common/oatbox/cache/KeyValueCache.php b/common/oatbox/cache/KeyValueCache.php new file mode 100644 index 000000000..f6eeefabd --- /dev/null +++ b/common/oatbox/cache/KeyValueCache.php @@ -0,0 +1,100 @@ + + * @package generis + */ +class KeyValueCache extends ConfigurableService implements SimpleCache +{ + use MultipleCacheTrait; + + const OPTION_PERSISTENCE = 'persistence'; + + /** @var common_persistence_KeyValuePersistence */ + private $persistence; + + public function set($key, $value, $ttl = null) + { + if ($ttl instanceof DateInterval) { + $ttl = $this->dateIntervalToSeconds($ttl); + } + return $this->getPersistence()->set($key, $value, $ttl); + } + + public function clear() + { + try { + return $this->getPersistence()->purge(); + } catch (common_exception_NotImplemented $e) { + return false; + } + } + + public function delete($key) + { + return $this->getPersistence()->del($key); + } + + public function get($key, $default = null) + { + $returnValue = $this->getPersistence()->get($key); + // persistence can return false on a value of false or a not found key + return ($returnValue !== false || $this->has($key)) + ? $returnValue + : $default; + } + + public function has($key) + { + return $this->getPersistence()->exists($key); + } + + protected function dateIntervalToSeconds(DateInterval $dateInterval): int + { + $reference = new DateTimeImmutable; + $endTime = $reference->add($dateInterval); + return $endTime->getTimestamp() - $reference->getTimestamp(); + } + + /** + * @return common_persistence_KeyValuePersistence + */ + protected function getPersistence() + { + if (is_null($this->persistence)) { + $this->persistence = $this->getServiceLocator()->get(PersistenceManager::SERVICE_ID)->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE)); + } + return $this->persistence; + } +} diff --git a/common/oatbox/cache/MultipleCacheTrait.php b/common/oatbox/cache/MultipleCacheTrait.php new file mode 100644 index 000000000..ffdef36a9 --- /dev/null +++ b/common/oatbox/cache/MultipleCacheTrait.php @@ -0,0 +1,61 @@ + + * @package generis + */ +trait MultipleCacheTrait +{ + + public function deleteMultiple($keys) + { + $success = true; + foreach ($keys as $key) { + $success = $this->delete($key) && $success; + } + return $success; + } + + public function getMultiple($keys, $default = null) + { + $values = []; + foreach ($keys as $key) { + $values[$key] = $this->get($key, $default); + } + return $values; + } + + public function setMultiple($values, $ttl = null) + { + $success = true; + foreach ($values as $key => $value) { + $success = $this->set($key, $value, $ttl) && $success; + } + return $success; + } + +} diff --git a/common/oatbox/cache/SimpleCache.php b/common/oatbox/cache/SimpleCache.php new file mode 100644 index 000000000..6c11bfd9b --- /dev/null +++ b/common/oatbox/cache/SimpleCache.php @@ -0,0 +1,29 @@ + 'cache' +]); diff --git a/test/unit/oatbox/cache/KeyValueCacheTest.php b/test/unit/oatbox/cache/KeyValueCacheTest.php new file mode 100644 index 000000000..2458c15ae --- /dev/null +++ b/test/unit/oatbox/cache/KeyValueCacheTest.php @@ -0,0 +1,107 @@ +cache = new KeyValueCache([KeyValueCache::OPTION_PERSISTENCE => 'unittest']); + $serviceLocator = $this->getServiceLocatorMock([ + PersistenceManager::SERVICE_ID => $this->getKeyValueMock('unittest') + ]); + $this->cache->setServiceLocator($serviceLocator); + } + + public function testGet() + { + $this->assertEquals(null, $this->cache->get('key1')); + $this->assertEquals('def', $this->cache->get('key1', 'def')); + } + + public function testSet() + { + $this->assertEquals(null, $this->cache->get('key1')); + $this->assertEquals(true, $this->cache->set('key1', 'value1')); + $this->assertEquals('value1', $this->cache->get('key1')); + $this->assertEquals('value1', $this->cache->get('key1', 'otherValue')); + } + + public function testDelete() + { + $this->assertEquals(12, $this->cache->get('key1', 12)); + $this->assertEquals(true, $this->cache->set('key1', 'value1')); + $this->assertEquals('value1', $this->cache->get('key1')); + $this->assertEquals(true, $this->cache->delete('key1')); + $this->assertEquals(12, $this->cache->get('key1', 12)); + } + + public function testClear() + { + $this->assertEquals(true, $this->cache->set('key1', 'value1')); + $this->assertEquals(true, $this->cache->set(12, 34)); + $this->assertEquals('value1', $this->cache->get('key1')); + $this->assertEquals(34, $this->cache->get(12)); + $this->assertEquals(true, $this->cache->clear()); + $this->assertEquals(null, $this->cache->get('key1')); + $this->assertEquals(null, $this->cache->get(12)); + } + + public function testHas() + { + $this->assertEquals(false, $this->cache->has('key1')); + $this->assertEquals(false, $this->cache->has(12)); + $this->assertEquals(true, $this->cache->set('key1', 'value1')); + $this->assertEquals(true, $this->cache->has('key1')); + $this->assertEquals(true, $this->cache->set(12, 34)); + $this->assertEquals(true, $this->cache->has(12)); + } + + public function testMultiple() + { + $this->assertEquals(['1' => null, '2' => null], $this->cache->getMultiple(['1', '2'])); + $this->assertEquals(['1' => 'a', '2' => 'a'], $this->cache->getMultiple(['1', '2'], 'a')); + $this->assertEquals(true, $this->cache->set('1', 'value1')); + $this->assertEquals(['1' => 'value1', '2' => 'a'], $this->cache->getMultiple(['1', '2'], 'a')); + $this->assertEquals(true, $this->cache->setMultiple(['2' => 'v2', '3' => 'v3'])); + $this->assertEquals(['1' => 'value1', '2' => 'v2', '3' => 'v3'], $this->cache->getMultiple(['1', '2', '3'])); + $this->assertEquals(true, $this->cache->deleteMultiple(['1', '3'])); + $this->assertEquals(['1' => 'x', '2' => 'v2', '3' => 'x'], $this->cache->getMultiple(['1', '2', '3'], 'x')); + $this->assertEquals(false, $this->cache->has(1)); + $this->assertEquals(true, $this->cache->has(2)); + $this->assertEquals(false, $this->cache->has(3)); + } +} From 919834bc70c5dcd984a29c640a7f131515404381 Mon Sep 17 00:00:00 2001 From: Bout Date: Tue, 12 May 2020 15:25:18 +0200 Subject: [PATCH 05/11] Wrap PSR 16 for current cache interface --- common/cache/class.PsrWrapperCache.php | 118 +++++++++++++++++++++++++ config/default/cache.conf.php | 4 +- 2 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 common/cache/class.PsrWrapperCache.php diff --git a/common/cache/class.PsrWrapperCache.php b/common/cache/class.PsrWrapperCache.php new file mode 100644 index 000000000..980a2d679 --- /dev/null +++ b/common/cache/class.PsrWrapperCache.php @@ -0,0 +1,118 @@ + + * @param mixed $mixed + * @param null $serial + * @param null $ttl + * @return bool + * @throws common_exception_Error + */ + public function put($mixed, $serial = null, $ttl = null) + { + if ($mixed instanceof common_Serializable) { + if (!is_null($serial) && $serial != $mixed->getSerial()) { + throw new common_exception_Error('Serial mismatch for Serializable ' . $mixed->getSerial()); + } + $serial = $mixed->getSerial(); + } + + return $this->getPsrSimpleCache()->set($serial, $mixed, $ttl); + } + + /** + * gets the entry associted to the serial + * + * @access public + * @author Jerome Bogaerts, + * @param string serial + * @return common_Serializable + * @throws common_cache_NotFoundException + */ + public function get($serial) + { + $returnValue = $this->getPsrSimpleCache()->get($serial, false); + if ($returnValue === false && !$this->getPsrSimpleCache()->has($serial)) { + $msg = "No cache entry found for '" . $serial . "'."; + throw new common_cache_NotFoundException($msg); + } + return $returnValue; + } + + /** + * test whenever an entry associated to the serial exists + * + * @access public + * @author Jerome Bogaerts, + * @param string serial + * @return boolean + */ + public function has($serial) + { + return $this->getPsrSimpleCache()->has($serial); + } + + /** + * removes an entry from the cache + * + * @access public + * @author Jerome Bogaerts, + * @param string serial + * @return mixed + */ + public function remove($serial) + { + return $this->getPsrSimpleCache()->delete($serial); + } + + /** + * empties the cache + * + * @access public + * @author Jerome Bogaerts, + * @return mixed + */ + public function purge() + { + return $this->getPsrSimpleCache()->clear(); + } + + protected function getPsrSimpleCache() : CacheInterface + { + return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID); + } +} diff --git a/config/default/cache.conf.php b/config/default/cache.conf.php index 97ebaf537..6cbcf4da0 100644 --- a/config/default/cache.conf.php +++ b/config/default/cache.conf.php @@ -4,6 +4,4 @@ * The default cache implementation */ -return new common_cache_KeyValueCache([ - common_cache_FileCache::OPTION_PERSISTENCE => 'cache' -]); +return new common_cache_PsrWrapperCache(); From 3013a25b1abdaa53b5506984f1671a9c4ec84df7 Mon Sep 17 00:00:00 2001 From: Bout Date: Tue, 12 May 2020 16:39:30 +0200 Subject: [PATCH 06/11] Version Bump --- manifest.php | 2 +- scripts/update/Updater.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/manifest.php b/manifest.php index 8f6983c50..983f23476 100755 --- a/manifest.php +++ b/manifest.php @@ -32,7 +32,7 @@ 'label' => 'Generis Core', 'description' => 'Core extension, provide the low level framework and an API to manage ontologies', 'license' => 'GPL-2.0', - 'version' => '12.21.1', + 'version' => '12.22.0', 'author' => 'Open Assessment Technologies, CRP Henri Tudor', 'requires' => [], 'install' => [ diff --git a/scripts/update/Updater.php b/scripts/update/Updater.php index 517dcbbe4..877d28ce6 100644 --- a/scripts/update/Updater.php +++ b/scripts/update/Updater.php @@ -56,6 +56,8 @@ use League\Flysystem\Adapter\Local; use oat\generis\model\kernel\uri\UriProvider; use oat\oatbox\config\ConfigurationService; +use oat\oatbox\cache\KeyValueCache; +use oat\oatbox\cache\SimpleCache; /** * @author Joel Bout @@ -508,5 +510,17 @@ public function update($initialVersion) } $this->skip('12.21.0', '12.21.1'); + + // register PSR 16 cache + if ($this->isVersion('12.21.1')) { + $oldCache = $this->getServiceManager()->get(\common_cache_Cache::SERVICE_ID); + $persistenceId = ($oldCache instanceof \common_cache_KeyValueCache) + ? $oldCache->getOption(\common_cache_KeyValueCache::OPTION_PERSISTENCE) + : 'cache' + ; + $psrCache = new KeyValueCache([KeyValueCache::OPTION_PERSISTENCE => $persistenceId]); + $this->getServiceManager()->register(SimpleCache::SERVICE_ID, $psrCache); + $this->setVersion('12.22.0'); + } } } From 16c41f8f95d1dc26a4a73c222ea998b45a9ebea5 Mon Sep 17 00:00:00 2001 From: Bout Date: Wed, 13 May 2020 13:33:18 +0200 Subject: [PATCH 07/11] Added deprecation warning --- common/cache/class.KeyValueCache.php | 1 + common/cache/class.PsrWrapperCache.php | 1 + 2 files changed, 2 insertions(+) diff --git a/common/cache/class.KeyValueCache.php b/common/cache/class.KeyValueCache.php index 7714a0d35..2be66c0bb 100644 --- a/common/cache/class.KeyValueCache.php +++ b/common/cache/class.KeyValueCache.php @@ -29,6 +29,7 @@ * @access public * @author Jerome Bogaerts, * @package generis + * @deprecated Please use oat\oatbox\cache\SimpleCache */ class common_cache_KeyValueCache extends ConfigurableService implements common_cache_Cache { diff --git a/common/cache/class.PsrWrapperCache.php b/common/cache/class.PsrWrapperCache.php index 980a2d679..3c2f212f9 100644 --- a/common/cache/class.PsrWrapperCache.php +++ b/common/cache/class.PsrWrapperCache.php @@ -25,6 +25,7 @@ /** * Wrap the PSR simple cache implementation into the legacy interface + * @deprecated Please use oat\oatbox\cache\SimpleCache */ class common_cache_PsrWrapperCache extends ConfigurableService implements common_cache_Cache { From d5b1233063413ae4c62f632f1aa747a4f9147293 Mon Sep 17 00:00:00 2001 From: Bout Date: Thu, 14 May 2020 08:41:07 +0200 Subject: [PATCH 08/11] Hardcode persistence id to be able to remove oldcache later --- scripts/update/Updater.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/scripts/update/Updater.php b/scripts/update/Updater.php index 877d28ce6..9fb8ee9b4 100644 --- a/scripts/update/Updater.php +++ b/scripts/update/Updater.php @@ -513,12 +513,7 @@ public function update($initialVersion) // register PSR 16 cache if ($this->isVersion('12.21.1')) { - $oldCache = $this->getServiceManager()->get(\common_cache_Cache::SERVICE_ID); - $persistenceId = ($oldCache instanceof \common_cache_KeyValueCache) - ? $oldCache->getOption(\common_cache_KeyValueCache::OPTION_PERSISTENCE) - : 'cache' - ; - $psrCache = new KeyValueCache([KeyValueCache::OPTION_PERSISTENCE => $persistenceId]); + $psrCache = new KeyValueCache([KeyValueCache::OPTION_PERSISTENCE => 'cache']); $this->getServiceManager()->register(SimpleCache::SERVICE_ID, $psrCache); $this->setVersion('12.22.0'); } From fc4fa033126813fd6b951ed30066ca05de3f3fce Mon Sep 17 00:00:00 2001 From: Bout Date: Thu, 14 May 2020 08:42:42 +0200 Subject: [PATCH 09/11] Use new cache for purging --- common/ext/class.ExtensionInstaller.php | 5 +++-- common/ext/class.ExtensionUninstaller.php | 6 ++++-- common/ext/class.GenerisInstaller.php | 3 ++- common/ext/class.UpdateExtensions.php | 3 ++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/common/ext/class.ExtensionInstaller.php b/common/ext/class.ExtensionInstaller.php index a31105349..e60af8b76 100755 --- a/common/ext/class.ExtensionInstaller.php +++ b/common/ext/class.ExtensionInstaller.php @@ -26,6 +26,7 @@ use oat\oatbox\event\EventManager; use oat\oatbox\service\ConfigurableService; use oat\generis\model\data\import\RdfImporter; +use oat\oatbox\cache\SimpleCache; /** * Generis installer of extensions @@ -84,8 +85,8 @@ public function install() // we purge the whole cache. $this->log('d', 'Purging cache...'); - $cache = common_cache_FileCache::singleton(); - $cache->purge(); + $cache = $this->getServiceManager()->get(SimpleCache::SERVICE_ID); + $cache->clear(); // check required extensions, throws exception if failed diff --git a/common/ext/class.ExtensionUninstaller.php b/common/ext/class.ExtensionUninstaller.php index 7777c33e5..ad57e80d4 100755 --- a/common/ext/class.ExtensionUninstaller.php +++ b/common/ext/class.ExtensionUninstaller.php @@ -1,4 +1,6 @@ unregister(); // we purge the whole cache. - $cache = common_cache_FileCache::singleton(); - $cache->purge(); + $cache = $this->getServiceManager()->get(SimpleCache::SERVICE_ID); + $cache->clear(); common_Logger::i('Uninstalled ' . $this->extension->getId()); return true; diff --git a/common/ext/class.GenerisInstaller.php b/common/ext/class.GenerisInstaller.php index 5ebd01ad2..7618b2806 100644 --- a/common/ext/class.GenerisInstaller.php +++ b/common/ext/class.GenerisInstaller.php @@ -24,6 +24,7 @@ use oat\generis\persistence\PersistenceManager; use oat\generis\model\data\Ontology; use oat\generis\persistence\sql\SchemaProviderInterface; +use oat\oatbox\cache\SimpleCache; /** * Custom extension installer for generis @@ -54,7 +55,7 @@ public function install() $this->installOntology(); $this->installRegisterExt(); - $this->getServiceManager()->get(common_cache_Cache::SERVICE_ID)->purge(); + $this->getServiceManager()->get(SimpleCache::SERVICE_ID)->clear(); $this->log('d', 'Installing custom script for extension ' . $this->extension->getId()); $this->installCustomScript(); diff --git a/common/ext/class.UpdateExtensions.php b/common/ext/class.UpdateExtensions.php index f72184cc1..8978576f6 100644 --- a/common/ext/class.UpdateExtensions.php +++ b/common/ext/class.UpdateExtensions.php @@ -25,6 +25,7 @@ use Zend\ServiceManager\ServiceLocatorAwareTrait; use Psr\Log\LoggerAwareInterface; use oat\oatbox\log\LoggerAwareTrait; +use oat\oatbox\cache\SimpleCache; /** * Run the extension updater @@ -121,7 +122,7 @@ protected function updateExtension(common_ext_Extension $ext) $report->add($versionReport); - common_cache_FileCache::singleton()->purge(); + $this->getServiceLocator()->get(SimpleCache::SERVICE_ID)->clear(); } } else { $report = new common_report_Report(common_report_Report::TYPE_INFO, $ext->getName() . ' already up to date'); From 061126ad186952f759d309ec0d1731f6ae8266d0 Mon Sep 17 00:00:00 2001 From: Bout Date: Thu, 14 May 2020 08:44:10 +0200 Subject: [PATCH 10/11] replace usage of old cache with new --- common/oatbox/action/ActionService.php | 24 +++----- common/oatbox/cache/NoCache.php | 61 +++++++++++++++++++ config/default/ontology.conf.php | 4 +- .../persistence/smoothsql/class.Property.php | 9 ++- .../smoothsql/class.SmoothModel.php | 19 +----- core/kernel/users/class.Cache.php | 19 +++--- test/OntologyMockTrait.php | 6 +- 7 files changed, 90 insertions(+), 52 deletions(-) create mode 100644 common/oatbox/cache/NoCache.php diff --git a/common/oatbox/action/ActionService.php b/common/oatbox/action/ActionService.php index 44e28634d..ea2760a70 100644 --- a/common/oatbox/action/ActionService.php +++ b/common/oatbox/action/ActionService.php @@ -22,8 +22,8 @@ namespace oat\oatbox\action; use oat\oatbox\service\ConfigurableService; -use oat\oatbox\service\ServiceNotFoundException; -use Zend\ServiceManager\ServiceLocatorAwareInterface; +use oat\oatbox\cache\SimpleCache; +use common_ext_ExtensionsManager; class ActionService extends ConfigurableService { @@ -43,9 +43,7 @@ public function resolve($actionIdentifier) $action = $this->getServiceManager()->get($actionIdentifier); } elseif (class_exists($actionIdentifier) && is_subclass_of($actionIdentifier, Action::class)) { $action = new $actionIdentifier(); - if ($action instanceof ServiceLocatorAwareInterface) { - $action->setServiceLocator($this->getServiceLocator()); - } + $this->propagate($action); } else { throw new ResolutionException('Unknown action ' . $actionIdentifier); } @@ -54,25 +52,21 @@ public function resolve($actionIdentifier) public function getAvailableActions() { - if ($this->getCache()->has(__FUNCTION__)) { - $actions = $this->getCache()->get(__FUNCTION__); - } else { + $actions = $this->getCache()->get(__FUNCTION__); + if (is_null($actions)) { $actions = []; - foreach (\common_ext_ExtensionsManager::singleton()->getInstalledExtensions() as $ext) { + foreach ($this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID)->getInstalledExtensions() as $ext) { $actions = array_merge($actions, $this->getActionsInDirectory($ext->getDir())); } $actions = array_merge($actions, $this->getActionsInDirectory(VENDOR_PATH . 'oat-sa')); - $this->getCache()->put($actions, __FUNCTION__); + $this->getCache()->set(__FUNCTION__, $actions); } return $actions; } - /** - * @return \common_cache_Cache - */ - protected function getCache() + protected function getCache(): SimpleCache { - return $this->getServiceManager()->get('generis/cache'); + return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID); } protected function getActionsInDirectory($dir) diff --git a/common/oatbox/cache/NoCache.php b/common/oatbox/cache/NoCache.php new file mode 100644 index 000000000..47feeee66 --- /dev/null +++ b/common/oatbox/cache/NoCache.php @@ -0,0 +1,61 @@ + + * @package generis + */ +class NoCache extends ConfigurableService implements SimpleCache +{ + use MultipleCacheTrait; + + public function set($key, $value, $ttl = null) + { + return true; + } + + public function clear() + { + return true; + } + + public function delete($key) + { + return true; + } + + public function get($key, $default = null) + { + return $default; + } + + public function has($key) + { + return false; + } +} diff --git a/config/default/ontology.conf.php b/config/default/ontology.conf.php index 063a12bdc..8ec96f4fb 100644 --- a/config/default/ontology.conf.php +++ b/config/default/ontology.conf.php @@ -1,7 +1,6 @@ SmoothModel::DEFAULT_WRITABLE_MODEL, - SmoothModel::OPTION_SEARCH_SERVICE => ComplexSearchService::SERVICE_ID, - SmoothModel::OPTION_CACHE_SERVICE => CommonCache::SERVICE_ID + SmoothModel::OPTION_SEARCH_SERVICE => ComplexSearchService::SERVICE_ID ]); \ No newline at end of file diff --git a/core/kernel/persistence/smoothsql/class.Property.php b/core/kernel/persistence/smoothsql/class.Property.php index 83e2a82aa..e4b67f4e4 100644 --- a/core/kernel/persistence/smoothsql/class.Property.php +++ b/core/kernel/persistence/smoothsql/class.Property.php @@ -59,15 +59,14 @@ class core_kernel_persistence_smoothsql_Property extends core_kernel_persistence */ public function isLgDependent(core_kernel_classes_Resource $resource) { - if ($this->getModel()->getCache()->has($resource->getUri())) { - $lgDependent = $this->getModel()->getCache()->get($resource->getUri()); - } else { - $lgDependentProperty = new \core_kernel_classes_Property(GenerisRdf::PROPERTY_IS_LG_DEPENDENT); + $lgDependent = $this->getModel()->getCache()->get($resource->getUri()); + if (is_null($lgDependent)) { + $lgDependentProperty = $this->getModel()->getProperty(GenerisRdf::PROPERTY_IS_LG_DEPENDENT); $lgDependentResource = $resource->getOnePropertyValue($lgDependentProperty); $lgDependent = !is_null($lgDependentResource) && $lgDependentResource instanceof \core_kernel_classes_Resource && $lgDependentResource->getUri() == GenerisRdf::GENERIS_TRUE; - $this->getModel()->getCache()->put($lgDependent, $resource->getUri()); + $this->getModel()->getCache()->set($resource->getUri(), $lgDependent); } return (bool) $lgDependent; } diff --git a/core/kernel/persistence/smoothsql/class.SmoothModel.php b/core/kernel/persistence/smoothsql/class.SmoothModel.php index 993771bd9..6109188d2 100755 --- a/core/kernel/persistence/smoothsql/class.SmoothModel.php +++ b/core/kernel/persistence/smoothsql/class.SmoothModel.php @@ -26,6 +26,7 @@ use oat\generis\persistence\sql\SchemaProviderInterface; use oat\generis\persistence\sql\SchemaCollection; use oat\generis\model\kernel\persistence\smoothsql\install\SmoothRdsModel; +use oat\oatbox\cache\SimpleCache; /** * transitory model for the smooth sql implementation @@ -44,12 +45,6 @@ class core_kernel_persistence_smoothsql_SmoothModel extends ConfigurableService const DEFAULT_WRITABLE_MODEL = 1; const DEFAULT_READ_ONLY_MODEL = 2; - /** - * Cache service to use - * @var string - */ - const OPTION_CACHE_SERVICE = 'cache'; - /** * Persistence to use for the smoothmodel * @@ -57,8 +52,6 @@ class core_kernel_persistence_smoothsql_SmoothModel extends ConfigurableService */ private $persistence; - private $cache; - function getResource($uri) { $resource = new \core_kernel_classes_Resource($uri); @@ -93,15 +86,9 @@ public function getPersistence() return $this->persistence; } - /** - * @return common_cache_Cache - */ - public function getCache() + public function getCache(): SimpleCache { - if (is_null($this->cache)) { - $this->cache = $this->getServiceLocator()->get($this->getOption(self::OPTION_CACHE_SERVICE)); - } - return $this->cache; + return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID); } /** diff --git a/core/kernel/users/class.Cache.php b/core/kernel/users/class.Cache.php index 14dc90671..753c25f9a 100644 --- a/core/kernel/users/class.Cache.php +++ b/core/kernel/users/class.Cache.php @@ -22,6 +22,7 @@ */ use oat\oatbox\service\ServiceManager; +use oat\oatbox\cache\SimpleCache; /** * A facade aiming at helping client code to put User data in the Cache memory. @@ -63,20 +64,18 @@ class core_kernel_users_Cache public static function retrieveIncludedRoles(core_kernel_classes_Resource $role) { $returnValue = []; - try { - $serial = self::buildIncludedRolesSerial($role); - $fromCache = ServiceManager::getServiceManager()->get('generis/cache')->get($serial); - // array of URIs. - - foreach ($fromCache as $uri) { - $returnValue[$uri] = new core_kernel_classes_Resource($uri); - } - } catch (common_cache_Exception $e) { + $serial = self::buildIncludedRolesSerial($role); + $fromCache = ServiceManager::getServiceManager()->get(SimpleCache::SERVICE_ID)->get($serial); + // array of URIs. + if (is_null($fromCache)) { $roleUri = $role->getUri(); $msg = "Includes roles related to Role with URI '${roleUri}' is not in the Cache memory."; throw new core_kernel_users_CacheException($msg); } - + + foreach ($fromCache as $uri) { + $returnValue[$uri] = new core_kernel_classes_Resource($uri); + } return (array) $returnValue; } diff --git a/test/OntologyMockTrait.php b/test/OntologyMockTrait.php index eaf1d8946..d117b7296 100644 --- a/test/OntologyMockTrait.php +++ b/test/OntologyMockTrait.php @@ -35,6 +35,8 @@ use core_kernel_persistence_smoothsql_SmoothModel; use oat\generis\model\kernel\persistence\newsql\NewSqlOntology; use oat\generis\persistence\sql\SchemaProviderInterface; +use oat\oatbox\cache\SimpleCache; +use oat\oatbox\cache\NoCache; trait OntologyMockTrait { @@ -48,7 +50,6 @@ protected function getNewSqlMock() NewSqlOntology::OPTION_READABLE_MODELS => [2,3], NewSqlOntology::OPTION_WRITEABLE_MODELS => [2], NewSqlOntology::OPTION_NEW_TRIPLE_MODEL => 2, - 'cache' => 'smoothcache' ]); return $this->setupOntology($model); } @@ -63,7 +64,6 @@ protected function getOntologyMock() core_kernel_persistence_smoothsql_SmoothModel::OPTION_READABLE_MODELS => [2,3], core_kernel_persistence_smoothsql_SmoothModel::OPTION_WRITEABLE_MODELS => [2], core_kernel_persistence_smoothsql_SmoothModel::OPTION_NEW_TRIPLE_MODEL => 2, - 'cache' => 'smoothcache' ]); return $this->setupOntology($model); } @@ -83,7 +83,7 @@ private function setupOntology(Ontology $onto) EventManager::SERVICE_ID => new EventManager(), LoggerService::SERVICE_ID => $this->prophesize(LoggerInterface::class)->reveal(), UriProvider::SERVICE_ID => new Bin2HexUriProvider([Bin2HexUriProvider::OPTION_NAMESPACE => 'http://ontology.mock/bin2hex#']), - 'smoothcache' => new \common_cache_NoCache() + SimpleCache::SERVICE_ID => new NoCache() ]); $session->setServiceLocator($sl); $onto->setServiceLocator($sl); From 265d31a8d57e48549a68f27174fcb1ae2474102e Mon Sep 17 00:00:00 2001 From: Bout Date: Thu, 14 May 2020 10:05:58 +0200 Subject: [PATCH 11/11] Added deprecation notice to cache --- common/cache/interface.Cache.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/common/cache/interface.Cache.php b/common/cache/interface.Cache.php index b1b9c25de..99e574d14 100644 --- a/common/cache/interface.Cache.php +++ b/common/cache/interface.Cache.php @@ -22,20 +22,7 @@ /** * basic interface a cache implementation has to implement - * - * @author Jerome Bogaerts, - * @package generis - - */ - - -/** - * basic interface a cache implementation has to implement - * - * @access public - * @author Jerome Bogaerts, - * @package generis - + * @deprecated please use oat\oatbox\cache\SimpleCache */ interface common_cache_Cache {