Better hash/cache handling for some functions

This commit is contained in:
the-djmaze 2023-03-13 15:21:37 +01:00
parent 321782aa26
commit 85b06123c6
3 changed files with 16 additions and 35 deletions

View file

@ -892,7 +892,7 @@ class Actions
$aResult['userLanguage'] = $this->ValidateLanguage($UserLanguageRaw, '', false, true); $aResult['userLanguage'] = $this->ValidateLanguage($UserLanguageRaw, '', false, true);
$aResult['PluginsLink'] = $this->oPlugins->HaveJs($bAdmin) $aResult['PluginsLink'] = $this->oPlugins->HaveJs($bAdmin)
? 'Plugins/0/' . ($bAdmin ? 'Admin' : 'User') . '/' . $this->StaticCache() . '/' ? 'Plugins/0/' . ($bAdmin ? 'Admin' : 'User') . '/' . $this->etag($this->oPlugins->Hash()) . '/'
: ''; : '';
$bAppJsDebug = $this->oConfig->Get('debug', 'javascript', false) $bAppJsDebug = $this->oConfig->Get('debug', 'javascript', false)
@ -1033,7 +1033,8 @@ class Actions
public function etag(string $sKey): string public function etag(string $sKey): string
{ {
return \md5($sKey . $this->oConfig->Get('cache', 'index', '')); // if ($sKey && $this->oConfig->Get('cache', 'enable', true) && $this->oConfig->Get('cache', 'http', true)) {
return \md5($sKey . $this->oConfig->Get('cache', 'index', '') . APP_VERSION);
} }
public function cacheByKey(string $sKey): bool public function cacheByKey(string $sKey): bool
@ -1095,15 +1096,6 @@ class Actions
*/ */
} }
public function StaticCache(): string
{
static $sCache = null;
if (!$sCache) {
$sCache = \md5(APP_VERSION . $this->oPlugins->Hash());
}
return $sCache;
}
public function SetActionParams(array $aCurrentActionParams, string $sMethodName = ''): self public function SetActionParams(array $aCurrentActionParams, string $sMethodName = ''): self
{ {
$this->oPlugins->RunHook('filter.action-params', array($sMethodName, &$aCurrentActionParams)); $this->oPlugins->RunHook('filter.action-params', array($sMethodName, &$aCurrentActionParams));

View file

@ -184,8 +184,9 @@ abstract class Service
$sCacheFileName = ''; $sCacheFileName = '';
if ($oConfig->Get('cache', 'system_data', true)) { if ($oConfig->Get('cache', 'system_data', true)) {
$sCacheFileName = 'TMPL:' . $sLanguage . \sha1( $sCacheFileName = 'TMPL:' . \sha1(
Utils::jsonEncode(array( Utils::jsonEncode(array(
$sLanguage,
$oConfig->Get('cache', 'index', ''), $oConfig->Get('cache', 'index', ''),
$oActions->Plugins()->Hash(), $oActions->Plugins()->Hash(),
$sAppJsMin, $sAppJsMin,

View file

@ -380,7 +380,6 @@ class ServiceActions
public function ServiceLang() : string public function ServiceLang() : string
{ {
// sleep(2);
$sResult = ''; $sResult = '';
\header('Content-Type: application/javascript; charset=utf-8'); \header('Content-Type: application/javascript; charset=utf-8');
@ -389,27 +388,22 @@ class ServiceActions
$sLanguage = $this->oActions->ValidateLanguage($this->aPaths[3], '', $bAdmin); $sLanguage = $this->oActions->ValidateLanguage($this->aPaths[3], '', $bAdmin);
$bCacheEnabled = $this->Config()->Get('cache', 'system_data', true); $bCacheEnabled = $this->Config()->Get('cache', 'system_data', true);
if (!empty($sLanguage) && $bCacheEnabled) {
$this->oActions->verifyCacheByKey($this->sQuery);
}
$sCacheFileName = ''; $sCacheFileName = '';
if ($bCacheEnabled) { if ($bCacheEnabled) {
$sCacheFileName = KeyPathHelper::LangCache( $sCacheFileName = KeyPathHelper::LangCache($sLanguage, $bAdmin, $this->oActions->Plugins()->Hash());
$sLanguage, $bAdmin, $this->oActions->Plugins()->Hash()); $this->oActions->verifyCacheByKey(\md5($sCacheFileName));
$sResult = $this->Cacher()->Get($sCacheFileName); $sResult = $this->Cacher()->Get($sCacheFileName);
} }
if (!\strlen($sResult)) { if (!\strlen($sResult)) {
$sResult = $this->oActions->compileLanguage($sLanguage, $bAdmin); $sResult = $this->oActions->compileLanguage($sLanguage, $bAdmin);
if ($bCacheEnabled && \strlen($sCacheFileName)) { if ($sCacheFileName) {
$this->Cacher()->Set($sCacheFileName, $sResult); $this->Cacher()->Set($sCacheFileName, $sResult);
} }
} }
if ($bCacheEnabled) { if ($sCacheFileName) {
$this->oActions->cacheByKey($this->sQuery); $this->oActions->cacheByKey(\md5($sCacheFileName));
} }
} }
@ -427,13 +421,10 @@ class ServiceActions
$sMinify = ($bAppDebug || $this->Config()->Get('debug', 'javascript', false)) ? '' : 'min'; $sMinify = ($bAppDebug || $this->Config()->Get('debug', 'javascript', false)) ? '' : 'min';
$bCacheEnabled = !$bAppDebug && $this->Config()->Get('cache', 'system_data', true); $bCacheEnabled = !$bAppDebug && $this->Config()->Get('cache', 'system_data', true);
if ($bCacheEnabled) {
$this->oActions->verifyCacheByKey($this->sQuery . $sMinify);
}
$sCacheFileName = ''; $sCacheFileName = '';
if ($bCacheEnabled) { if ($bCacheEnabled) {
$sCacheFileName = KeyPathHelper::PluginsJsCache($this->oActions->Plugins()->Hash()) . $sMinify; $sCacheFileName = KeyPathHelper::PluginsJsCache($this->oActions->Plugins()->Hash()) . $sMinify;
$this->oActions->verifyCacheByKey(\md5($sCacheFileName));
$sResult = $this->Cacher()->Get($sCacheFileName); $sResult = $this->Cacher()->Get($sCacheFileName);
} }
@ -444,8 +435,8 @@ class ServiceActions
} }
} }
if ($bCacheEnabled) { if ($sCacheFileName) {
$this->oActions->cacheByKey($this->sQuery . $sMinify); $this->oActions->cacheByKey(\md5($sCacheFileName));
} }
return $sResult; return $sResult;
@ -471,13 +462,10 @@ class ServiceActions
$sMinify = ($bAppDebug || $this->Config()->Get('debug', 'css', false)) ? '' : 'min'; $sMinify = ($bAppDebug || $this->Config()->Get('debug', 'css', false)) ? '' : 'min';
$bCacheEnabled = !$bAppDebug && $this->Config()->Get('cache', 'system_data', true); $bCacheEnabled = !$bAppDebug && $this->Config()->Get('cache', 'system_data', true);
if ($bCacheEnabled) {
$this->oActions->verifyCacheByKey($this->sQuery . $sMinify);
}
$sCacheFileName = ''; $sCacheFileName = '';
if ($bCacheEnabled) { if ($bCacheEnabled) {
$sCacheFileName = KeyPathHelper::CssCache($sTheme, $this->oActions->Plugins()->Hash()) . $sMinify; $sCacheFileName = KeyPathHelper::CssCache($sTheme, $this->oActions->Plugins()->Hash()) . $sMinify;
$this->oActions->verifyCacheByKey(\md5($sCacheFileName . ($bJson ? 1 : 0)));
$sResult = $this->Cacher()->Get($sCacheFileName); $sResult = $this->Cacher()->Get($sCacheFileName);
} }
@ -495,8 +483,8 @@ class ServiceActions
} }
} }
if ($bCacheEnabled) { if ($sCacheFileName) {
$this->oActions->cacheByKey($this->sQuery . $sMinify); $this->oActions->cacheByKey(\md5($sCacheFileName . ($bJson ? 1 : 0 )));
} }
} }