N°8772 - cache now accept subdirectories

This commit is contained in:
Eric Espie
2025-12-16 15:41:55 +01:00
parent b653236e86
commit bd20818b66
3 changed files with 40 additions and 7 deletions

View File

@@ -52,12 +52,13 @@ class FormBlockService
if (strlen($sFilteredId) === 0 || $sFilteredId !== $sId) {
throw new FormBlockException('Malformed name for block: '.json_encode($sId));
}
if (!$this->oCacheService->HasEntry(self::CACHE_POOL, $sFilteredId) || utils::IsDevelopmentEnvironment()) {
$sCacheKey = $sType.'/'.$sFilteredId;
if (!$this->oCacheService->HasEntry(self::CACHE_POOL, $sCacheKey) || utils::IsDevelopmentEnvironment()) {
// Cache not found, compile the form
$sPHPContent = FormsCompiler::GetInstance()->CompileForm($sFilteredId, $sType);
$this->oCacheService->StorePhpContent(FormBlockService::CACHE_POOL, $sFilteredId, "<?php\n\n$sPHPContent");
$this->oCacheService->StorePhpContent(FormBlockService::CACHE_POOL, $sCacheKey, "<?php\n\n$sPHPContent");
}
$this->oCacheService->Fetch(self::CACHE_POOL, $sFilteredId);
$this->oCacheService->FetchPHP(self::CACHE_POOL, $sCacheKey);
$sFormBlockClass = 'FormFor__'.$sFilteredId;
return new $sFormBlockClass($sFilteredId);

View File

@@ -90,9 +90,25 @@ class DataModelDependantCache
if (!is_file($sCacheFileName)) {
return null;
}
return include_once $sCacheFileName;
return include $sCacheFileName;
}
/**
* Includes the cached PHP code for a given key, in the current pool.
* Nothing is done when the key is not found.
*
* @param string $sPool
* @param string $sKey
*/
public function FetchPHP(string $sPool, string $sKey): void
{
$sCacheFileName = $this->MakeCacheFileName($sPool, $sKey);
if (!is_file($sCacheFileName)) {
return;
}
include_once $sCacheFileName;
}
/**
* @param string $sPool
* @param string $sKey
@@ -155,7 +171,7 @@ class DataModelDependantCache
private function MakeCacheFileName(string $sPool, string $sKey): string
{
// Replace all characters that are not alphanumeric by '_'
$sKey = preg_replace('/[^a-zA-Z0-9]/', '_', $sKey);
$sKey = preg_replace('/[^a-zA-Z0-9\/]/', '_', $sKey);
return $this->MakePoolDirPath($sPool).$sKey.'.php';
}

View File

@@ -156,10 +156,26 @@ class DataModelDependantCacheTest extends ItopTestCase
$this->assertEquals($iRefTime, $this->oCacheService->GetEntryModificationTime('pool-A', 'key'), 'GetEntryModificationTime should return the modification time of the cache file');
$this->assertEquals(null, $this->oCacheService->GetEntryModificationTime('pool-A', 'non-existing-key'), 'GetEntryModificationTime should return null for an invalid key');
}
public function testKeyUndesiredCharactersShouldBeTransformedToUnderscore()
{
$sUglyKey = 'key with ugly characters:\{&"#@ç^²/,;[(|🤔';
$sUglyKey = 'key with ugly characters:\{&"#@ç^²,;[(|🤔';
$sFilePath = $this->InvokeNonPublicMethod(DataModelDependantCache::class, 'MakeCacheFileName', $this->oCacheService, ['pool-A', $sUglyKey]);
$this->assertEquals('key_with_ugly_characters______________________.php', basename($sFilePath));
$this->assertEquals('key_with_ugly_characters_____________________.php', basename($sFilePath));
}
public function testKeyCanBeADirectoryTree()
{
$sBaseKey = 'test';
$sBaseFilePath = $this->InvokeNonPublicMethod(DataModelDependantCache::class, 'MakeCacheFileName', $this->oCacheService, ['pool-A', $sBaseKey]);
$sKey = 'Path/To/KeyCanBePath';
$sFilePath = $this->InvokeNonPublicMethod(DataModelDependantCache::class, 'MakeCacheFileName', $this->oCacheService, ['pool-A', $sKey]);
$this->assertEquals(dirname($sBaseFilePath).'/Path/To', dirname($sFilePath));
$this->oCacheService->Store('pool-A', $sKey, 'some data...');
$this->assertTrue($this->oCacheService->HasEntry('pool-A', $sKey), 'The data should have been stored');
$this->assertFileExists($sFilePath, 'A file should have been created in the corresponding directory');
}
}