diff --git a/Classes/Command/ReindexCommand.php b/Classes/Command/ReindexCommand.php index ac04af65f..5701b57ef 100644 --- a/Classes/Command/ReindexCommand.php +++ b/Classes/Command/ReindexCommand.php @@ -13,7 +13,7 @@ namespace Kitodo\Dlf\Command; use Kitodo\Dlf\Common\AbstractDocument; -use Kitodo\Dlf\Command\BaseCommand; +use Kitodo\Dlf\Common\DocumentCacheManager; use Kitodo\Dlf\Common\Indexer; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -224,7 +224,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int Indexer::add($document, $this->documentRepository, $input->getOption('softCommit')); } // Clear document cache to prevent memory exhaustion. - AbstractDocument::clearDocumentCache(); + GeneralUtility::makeInstance(DocumentCacheManager::class)->flush(); } // Clear state of persistence manager to prevent memory exhaustion. diff --git a/Classes/Common/AbstractDocument.php b/Classes/Common/AbstractDocument.php index 42901fc8e..c30315b36 100644 --- a/Classes/Common/AbstractDocument.php +++ b/Classes/Common/AbstractDocument.php @@ -12,7 +12,6 @@ namespace Kitodo\Dlf\Common; -use TYPO3\CMS\Core\Cache\CacheManager; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Log\Logger; @@ -536,7 +535,7 @@ public static function &getInstance(string $location, array $settings = [], bool $iiif = null; if (!$forceReload) { - $instance = self::getDocumentCache($location); + $instance = GeneralUtility::makeInstance(DocumentCacheManager::class)->get($location); if ($instance !== false) { return $instance; } @@ -584,27 +583,12 @@ public static function &getInstance(string $location, array $settings = [], bool } if ($instance !== null) { - self::setDocumentCache($location, $instance); + GeneralUtility::makeInstance(DocumentCacheManager::class)->set($location, $instance); } return $instance; } - /** - * Clear document cache. - * - * @access public - * - * @static - * - * @return void - */ - public static function clearDocumentCache(): void - { - $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('tx_dlf_doc'); - $cache->flush(); - } - /** * This returns the first corresponding physical page number of a given logical page label * @@ -1260,45 +1244,4 @@ public function __set(string $var, $value): void $this->$method($value); } } - - /** - * Get Cache Hit for document instance - * - * @access private - * - * @static - * - * @param string $location - * - * @return AbstractDocument|false - */ - private static function getDocumentCache(string $location) - { - $cacheIdentifier = hash('md5', $location); - $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('tx_dlf_doc'); - $cacheHit = $cache->get($cacheIdentifier); - - return $cacheHit; - } - - /** - * Set Cache for document instance - * - * @access private - * - * @static - * - * @param string $location - * @param AbstractDocument $currentDocument - * - * @return void - */ - private static function setDocumentCache(string $location, AbstractDocument $currentDocument): void - { - $cacheIdentifier = hash('md5', $location); - $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('tx_dlf_doc'); - - // Save value in cache - $cache->set($cacheIdentifier, $currentDocument); - } } diff --git a/Classes/Common/DocumentCacheManager.php b/Classes/Common/DocumentCacheManager.php new file mode 100644 index 000000000..8717be499 --- /dev/null +++ b/Classes/Common/DocumentCacheManager.php @@ -0,0 +1,110 @@ + + * + * This file is part of the Kitodo and TYPO3 projects. + * + * @license GNU General Public License version 3 or later. + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + */ + +namespace Kitodo\Dlf\Common; + +use TYPO3\CMS\Core\Cache\CacheManager; +use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; +use TYPO3\CMS\Core\Utility\GeneralUtility; + +/** + * DocumentCacheManager class for the 'dlf' extension + * + * @package TYPO3 + * @subpackage dlf + * + * @access public + */ +class DocumentCacheManager +{ + /** + * @var FrontendInterface + */ + protected $cache; + + /** + * Constructor + */ + public function __construct() + { + $this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('tx_dlf_doc'); + } + + /** + * Get document instance from cache or false if not found. + * + * @access public + * + * @param string $location + * + * @return AbstractDocument|false + */ + public function get(string $location) + { + return $this->cache->get($this->getIdentifier($location)); + } + + /** + * Remove all documents from cache. + * + * @access public + * + * @return void + */ + public function flush(): void + { + $this->cache->flush(); + } + + /** + * Remove single document from cache. + * + * @access public + * + * @param string $location + * + * @return void + */ + public function remove(string $location): void + { + $this->cache->remove($this->getIdentifier($location)); + } + + /** + * Set cache for document instance. + * + * @access public + * + * @param string $location + * @param AbstractDocument $currentDocument + * + * @return void + */ + public function set(string $location, AbstractDocument $currentDocument): void + { + $this->cache->set($this->getIdentifier($location), $currentDocument); + } + + /** + * Get cache identifier for document location. + * + * @access private + * + * @param string $location + * + * @return string + */ + private function getIdentifier(string $location): string + { + return hash('md5', $location); + } +}