Skip to content

Commit

Permalink
[FEATURE] Implement document cache manager (#1401)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
beatrycze-volk and sebastian-meyer authored Dec 16, 2024
1 parent 7b95dff commit c6b4973
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Classes/Command/ReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
61 changes: 2 additions & 59 deletions Classes/Common/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
}
110 changes: 110 additions & 0 deletions Classes/Common/DocumentCacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* (c) Kitodo. Key to digital objects e.V. <[email protected]>
*
* 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);
}
}

0 comments on commit c6b4973

Please sign in to comment.