From c494e5ba6c062153a464b59f160cbe596492d1af Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Fri, 13 Apr 2018 12:42:51 +0200 Subject: [PATCH] Add ReactionTypeCache See #2508 --- .../reaction/type/ReactionTypeCache.class.php | 92 +++++++++++++++++++ .../type/ReactionTypeEditor.class.php | 2 +- .../ReactionTypeCacheBuilder.class.php | 29 ++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 wcfsetup/install/files/lib/data/reaction/type/ReactionTypeCache.class.php create mode 100644 wcfsetup/install/files/lib/system/cache/builder/ReactionTypeCacheBuilder.class.php diff --git a/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeCache.class.php b/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeCache.class.php new file mode 100644 index 0000000000..62be6f714c --- /dev/null +++ b/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeCache.class.php @@ -0,0 +1,92 @@ + + * @package WoltLabSuite\Core\Data\Reaction\Type + * @since 3.2 + */ +class ReactionTypeCache extends SingletonFactory { + /** + * Contains all reaction types. + * @var ReactionType[] + */ + protected $reactionTypes; + + /** + * Contains all enabled reaction types. + * @var ReactionType[] + */ + protected $enabledReactionTypes; + + /** + * @inheritDoc + */ + public function init() { + $this->reactionTypes = ReactionTypeCacheBuilder::getInstance()->getData(); + $this->enabledReactionTypes = ReactionTypeCacheBuilder::getInstance()->getData(['onlyEnabled' => 1]); + } + + /** + * Returns the reaction type with the given reactionTypeID. + * + * @param integer $trophyID + * @return ReactionType + */ + public function getReactionTypeByID($trophyID): ReactionType { + if (isset($this->reactionTypes[$trophyID])) { + return $this->reactionTypes[$trophyID]; + } + + return null; + } + + /** + * Returns the reaction types with the given reactionTypeIDs. + * + * @param integer[] $reactionTypeIDs + * @return ReactionType[] + */ + public function getReactionTypesByID(array $reactionTypeIDs): array { + $returnValues = []; + + foreach ($reactionTypeIDs as $reactionType) { + $returnValues[] = $this->getReactionTypeByID($reactionType); + } + + return $returnValues; + } + + /** + * Return all reaction types. + * + * @return ReactionType[] + */ + public function getReactionTypes(): array { + return $this->reactionTypes; + } + + /** + * Return all enabled reaction types. + * + * @return ReactionType[] + */ + public function getEnabledReactionTypes(): array { + return $this->enabledReactionTypes; + } + + /** + * Resets the cache for the trophies. + */ + public function clearCache() { + ReactionTypeCacheBuilder::getInstance()->reset(); + ReactionTypeCacheBuilder::getInstance()->reset(['onlyEnabled' => 1]); + } +} diff --git a/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeEditor.class.php b/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeEditor.class.php index b026a6bece..53c6bbb52b 100644 --- a/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeEditor.class.php +++ b/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeEditor.class.php @@ -25,6 +25,6 @@ class ReactionTypeEditor extends DatabaseObjectEditor implements IEditableCached * @inheritDoc */ public static function resetCache() { - // @TODO + ReactionTypeCache::getInstance()->clearCache(); } } diff --git a/wcfsetup/install/files/lib/system/cache/builder/ReactionTypeCacheBuilder.class.php b/wcfsetup/install/files/lib/system/cache/builder/ReactionTypeCacheBuilder.class.php new file mode 100644 index 0000000000..edc09b816d --- /dev/null +++ b/wcfsetup/install/files/lib/system/cache/builder/ReactionTypeCacheBuilder.class.php @@ -0,0 +1,29 @@ + + * @package WoltLabSuite\Core\System\Cache\Builder + * @since 3.2 + */ +class ReactionTypeCacheBuilder extends AbstractCacheBuilder { + /** + * @inheritDoc + */ + public function rebuild(array $parameters): array { + $reactionTypeList = new ReactionTypeList(); + + if (isset($parameters['onlyEnabled']) && $parameters['onlyEnabled']) { + $reactionTypeList->getConditionBuilder()->add('isDisabled = ?', [0]); + } + + $reactionTypeList->readObjects(); + return $reactionTypeList->getObjects(); + } +} -- 2.20.1