Skip to content

Commit

Permalink
OXDEV-8412 Replace utils file cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AshrafOxid committed Dec 10, 2024
1 parent 1794691 commit 4966943
Show file tree
Hide file tree
Showing 18 changed files with 386 additions and 461 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG-8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
- Support PSR caching interface, related functionalities and applied them on module cache.
- Registration of environment variables via Symfony Dotenv Component
- Interface for storing Symfony Service Container parameters in configuration
- Support Symfony caching interface with tags

### Deprecated

- `Utils` methods for managing cache will be replaced by using Symfony cache directly

### Changed

Expand All @@ -25,6 +30,8 @@
All corresponding command-line parameters were removed
- Updated list of Search Engines (formerly `aRobots` configuration)
- Browser-based application setup was discontinued. Only console-based setup is available
- Replace file caching in `Utils` with Symfony cache
- Removed $includePermanentCache parameter from `oxResetFileCache` method, all cache files are now cleared without exclusions.

### Removed

Expand Down Expand Up @@ -56,4 +63,5 @@
container.
- Deprecated `handleDatabaseException` functionality
- Dependency on `oxideshop-facts` component
- `FileCache` and `SubShopSpecificFileCache` classes. Use `ContextInterface::getCurrentShopId()` instead
- `FileCache` and `SubShopSpecificFileCache` classes. Use `ContextInterface::getCurrentShopId()` instead
- Legacy file-based caching methods from `Utils` class
106 changes: 59 additions & 47 deletions source/Application/Controller/Admin/NavigationTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use OxidEsales\Eshop\Core\Str;
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
use stdClass;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

class NavigationTree extends Base
{
Expand Down Expand Up @@ -393,60 +395,38 @@ protected function processCachedFile($cacheContents)
return $cacheContents;
}

/**
* get initial dom, not modified by init method
*
* @return DOMDocument
*/
protected function getInitialDom()
{
if ($this->_oInitialDom === null) {
$myOxUtlis = \OxidEsales\Eshop\Core\Registry::getUtils();

if (is_array($filesToLoad = $this->getMenuFiles())) {
// now checking if xml files are newer than cached file
$reload = false;
$templateLanguageCode = $this->getTemplateLanguageCode();

$shopId = \OxidEsales\Eshop\Core\Registry::getConfig()->getActiveShop()->getShopId();
$cacheName = 'menu_' . $templateLanguageCode . $shopId . '_xml';
$cacheFile = $myOxUtlis->getCacheFilePath($cacheName);
$cacheContents = $myOxUtlis->fromFileCache($cacheName);
if ($cacheContents && file_exists($cacheFile) && ($cacheModTime = filemtime($cacheFile))) {
foreach ($filesToLoad as $dynPath) {
if ($cacheModTime < filemtime($dynPath)) {
$reload = true;
}
}
} else {
$reload = true;
}
if ($this->_oInitialDom !== null) {
return $this->_oInitialDom;
}

$this->_oInitialDom = new DOMDocument();
if ($reload) {
// fully reloading and building pathes
$this->_oInitialDom->appendChild(new DOMElement('OX'));
$filesToLoad = $this->getMenuFiles();
if (!is_array($filesToLoad)) {
return null;
}

foreach ($filesToLoad as $dynPath) {
$this->loadFromFile($dynPath, $this->_oInitialDom);
}
$templateLanguageCode = $this->getTemplateLanguageCode();
$cacheName = 'shop_menu_cache_' . $templateLanguageCode;
$cache = ContainerFacade::get(TagAwareCacheInterface::class);

// adds links to menu items
$this->addLinks($this->_oInitialDom);
if ($this->isMenuCacheOutdated($cache, $cacheName, $filesToLoad)) {
$cache->delete($cacheName);
}

// writing to cache
$myOxUtlis->toFileCache($cacheName, $this->_oInitialDom->saveXML());
} else {
$cacheContents = $this->processCachedFile($cacheContents);
// loading from cached file
$this->_oInitialDom->preserveWhiteSpace = false;
$this->_oInitialDom->loadXML($cacheContents);
}
$cacheContents = $cache->get($cacheName, function (ItemInterface $item) use ($filesToLoad): array {
$item->tag('oxid_esales.cache.menu');
return [
'creation_time' => time(),
'menu_dom' => $this->generateInitialMenuDomXml($filesToLoad)
];
});

// add session params
$this->sessionizeLocalUrls($this->_oInitialDom);
}
}
$this->_oInitialDom = new DOMDocument();
$this->_oInitialDom->preserveWhiteSpace = false;
$this->_oInitialDom->loadXML($cacheContents['menu_dom']);

$this->sessionizeLocalUrls($this->_oInitialDom);

return $this->_oInitialDom;
}
Expand Down Expand Up @@ -643,4 +623,36 @@ protected function getTemplateLanguageCode()
protected function onGettingDomXml()
{
}

private function isMenuCacheOutdated($cache, string $cacheName, array $filesToLoad): bool
{
$cacheItem = $cache->getItem($cacheName);

if (!$cacheItem->isHit()) {
return true;
}

$cacheCreationTime = $cacheItem->get()['creation_time'];
foreach ($filesToLoad as $filePath) {
if ($cacheCreationTime < filemtime($filePath)) {
return true;
}
}

return false;
}

private function generateInitialMenuDomXml(array $filesToLoad): string
{
$initialDom = new DOMDocument();
$initialDom->appendChild(new DOMElement('OX'));

foreach ($filesToLoad as $filePath) {
$this->loadFromFile($filePath, $initialDom);
}

$this->addLinks($initialDom);

return $initialDom->saveXML();
}
}
3 changes: 0 additions & 3 deletions source/Application/Controller/OxidStartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ public function pageClose()
if (isset($session)) {
$session->freeze();
}

//commit file cache
\OxidEsales\Eshop\Core\Registry::getUtils()->commitFileCache();
}

/**
Expand Down
1 change: 0 additions & 1 deletion source/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,6 @@ public function getDir($file, $dir, $admin, $lang = null, $shop = null, $theme =

// TODO: implement logic to log missing paths

// to cache
Registry::getUtils()->toStaticCache($cacheKey, $return);

return $return;
Expand Down
38 changes: 15 additions & 23 deletions source/Core/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Str;
use stdClass;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

/**
* Language related utility class
Expand Down Expand Up @@ -724,7 +726,7 @@ protected function getCustomThemeLanguageFiles($language)
$aLangFiles = [];

if ($sCustomTheme) {
$customThemePath = $sAppDir . 'views/' . $sCustomTheme .'/';
$customThemePath = $sAppDir . 'views/' . $sCustomTheme . '/';
$aLangFiles = array_merge($aLangFiles, $this->getThemeLanguageFiles($customThemePath, $sLang));
}

Expand Down Expand Up @@ -893,29 +895,20 @@ protected function getLangFileCacheName($blAdmin, $iLang, $aLangFiles = null)
return "langcache_" . ((int) $blAdmin) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam('sTheme') . $sLangFilesIdent;
}

/**
* Returns language cache array
*
* @param bool $blAdmin admin or not [optional]
* @param int $iLang current language id [optional]
* @param array $aLangFiles language files to load [optional]
*
* @return array
*/
protected function getLanguageFileData($blAdmin = false, $iLang = 0, $aLangFiles = null)
{
$myUtils = Registry::getUtils();

$sCacheName = $this->getLangFileCacheName($blAdmin, $iLang, $aLangFiles);
$aLangCache = $myUtils->getLangCache($sCacheName);
if (!$aLangCache && $aLangFiles === null) {
if ($blAdmin) {
$aLangFiles = $this->getAdminLangFilesPathArray($iLang);
} else {
$aLangFiles = $this->getLangFilesPathArray($iLang);

$cache = ContainerFacade::get(TagAwareCacheInterface::class);
$aLangCache = $cache->get($sCacheName, function (ItemInterface $item) use ($aLangFiles, $blAdmin, $iLang): array {
$item->tag('oxid_esales.cache.language');
if ($aLangFiles === null) {
if ($blAdmin) {
$aLangFiles = $this->getAdminLangFilesPathArray($iLang);
} else {
$aLangFiles = $this->getLangFilesPathArray($iLang);
}
}
}
if (!$aLangCache && $aLangFiles) {
$aLangCache = [];
$sBaseCharset = $this->getTranslationsExpectedEncoding();
$aLang = [];
Expand All @@ -941,9 +934,8 @@ protected function getLanguageFileData($blAdmin = false, $iLang = 0, $aLangFiles
// special character replacement list
$aLangCache['_aSeoReplaceChars'] = $aLangSeoReplaceChars;

//save to cache
$myUtils->setLangCache($sCacheName, $aLangCache);
}
return $aLangCache;
});

return $aLangCache;
}
Expand Down
Loading

0 comments on commit 4966943

Please sign in to comment.