Skip to content

Commit 5000e86

Browse files
committed
Merge branch 'release/0.6.45'
2 parents 677103d + 2a63d82 commit 5000e86

File tree

4 files changed

+210
-33
lines changed

4 files changed

+210
-33
lines changed

.version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"strategy": "semver",
33
"major": 0,
44
"minor": 6,
5-
"patch": 44,
5+
"patch": 45,
66
"build": 0
77
}

src/Mvc/Controllers/Base.php

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ protected function getViewCache( string $Page, array $Data = [] ): ?string
252252
return $ViewCache->get( $CacheKey );
253253
}
254254

255+
/**
256+
* Check if cache is enabled by default in system settings.
257+
*
258+
* @return bool True if cache is enabled in settings, false otherwise
259+
*/
260+
protected function isCacheEnabledByDefault(): bool
261+
{
262+
$ViewCache = $this->initializeViewCache();
263+
return $ViewCache && $ViewCache->isEnabled();
264+
}
265+
255266
/**
256267
* Check if view cache exists using only cache key data.
257268
* This allows checking cache without fetching full view data.
@@ -328,8 +339,12 @@ public function renderHtmlWithCacheKey(
328339
{
329340
@http_response_code( $ResponseCode->value );
330341

331-
// If view data is empty and cache is enabled, try to get cached content
332-
if( empty( $ViewData ) && $CacheEnabled !== false )
342+
// Determine if cache should be used based on explicit setting or system default
343+
$ShouldUseCache = $CacheEnabled !== false &&
344+
( $CacheEnabled === true || $this->isCacheEnabledByDefault() );
345+
346+
// If view data is empty and cache should be used, try to get cached content
347+
if( empty( $ViewData ) && $ShouldUseCache )
333348
{
334349
$CachedContent = $this->getViewCacheByKey( $Page, $CacheKeyData );
335350
if( $CachedContent !== null )
@@ -350,24 +365,41 @@ public function renderHtmlWithCacheKey(
350365
{
351366
$RenderedContent = $View->render( $ViewData );
352367

353-
// Store in cache using cache key data
354-
if( $CacheEnabled === true )
368+
// Store in cache using cache key data if cache should be used
369+
if( $ShouldUseCache )
355370
{
356371
$ViewCache = $this->initializeViewCache();
357-
if( $ViewCache && $ViewCache->isEnabled() )
372+
if( $ViewCache )
358373
{
359-
$CacheKey = $ViewCache->generateKey(
360-
$this->getControllerName(),
361-
$Page,
362-
$CacheKeyData
363-
);
364-
try
365-
{
366-
$ViewCache->set( $CacheKey, $RenderedContent );
367-
}
368-
catch( CacheException $e )
374+
// When cache is explicitly enabled, bypass global check
375+
if( $CacheEnabled === true || $ViewCache->isEnabled() )
369376
{
370-
// Silently fail on cache write errors
377+
$CacheKey = $ViewCache->generateKey(
378+
$this->getControllerName(),
379+
$Page,
380+
$CacheKeyData
381+
);
382+
try
383+
{
384+
// Temporarily enable cache if needed for storage
385+
$WasEnabled = $ViewCache->isEnabled();
386+
if( $CacheEnabled === true && !$WasEnabled )
387+
{
388+
$ViewCache->setEnabled( true );
389+
}
390+
391+
$ViewCache->set( $CacheKey, $RenderedContent );
392+
393+
// Restore original state
394+
if( $CacheEnabled === true && !$WasEnabled )
395+
{
396+
$ViewCache->setEnabled( false );
397+
}
398+
}
399+
catch( CacheException $e )
400+
{
401+
// Silently fail on cache write errors
402+
}
371403
}
372404
}
373405
}
@@ -405,8 +437,12 @@ public function renderMarkdownWithCacheKey(
405437
{
406438
@http_response_code( $ResponseCode->value );
407439

408-
// If view data is empty and cache is enabled, try to get cached content
409-
if( empty( $ViewData ) && $CacheEnabled !== false )
440+
// Determine if cache should be used based on explicit setting or system default
441+
$ShouldUseCache = $CacheEnabled !== false &&
442+
( $CacheEnabled === true || $this->isCacheEnabledByDefault() );
443+
444+
// If view data is empty and cache should be used, try to get cached content
445+
if( empty( $ViewData ) && $ShouldUseCache )
410446
{
411447
$CachedContent = $this->getViewCacheByKey( $Page, $CacheKeyData );
412448
if( $CachedContent !== null )
@@ -427,24 +463,41 @@ public function renderMarkdownWithCacheKey(
427463
{
428464
$RenderedContent = $View->render( $ViewData );
429465

430-
// Store in cache using cache key data
431-
if( $CacheEnabled === true )
466+
// Store in cache using cache key data if cache should be used
467+
if( $ShouldUseCache )
432468
{
433469
$ViewCache = $this->initializeViewCache();
434-
if( $ViewCache && $ViewCache->isEnabled() )
470+
if( $ViewCache )
435471
{
436-
$CacheKey = $ViewCache->generateKey(
437-
$this->getControllerName(),
438-
$Page,
439-
$CacheKeyData
440-
);
441-
try
442-
{
443-
$ViewCache->set( $CacheKey, $RenderedContent );
444-
}
445-
catch( CacheException $e )
472+
// When cache is explicitly enabled, bypass global check
473+
if( $CacheEnabled === true || $ViewCache->isEnabled() )
446474
{
447-
// Silently fail on cache write errors
475+
$CacheKey = $ViewCache->generateKey(
476+
$this->getControllerName(),
477+
$Page,
478+
$CacheKeyData
479+
);
480+
try
481+
{
482+
// Temporarily enable cache if needed for storage
483+
$WasEnabled = $ViewCache->isEnabled();
484+
if( $CacheEnabled === true && !$WasEnabled )
485+
{
486+
$ViewCache->setEnabled( true );
487+
}
488+
489+
$ViewCache->set( $CacheKey, $RenderedContent );
490+
491+
// Restore original state
492+
if( $CacheEnabled === true && !$WasEnabled )
493+
{
494+
$ViewCache->setEnabled( false );
495+
}
496+
}
497+
catch( CacheException $e )
498+
{
499+
// Silently fail on cache write errors
500+
}
448501
}
449502
}
450503
}

tests/Mvc/Controllers/CacheKeyDataTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,127 @@ public function testRenderWithCacheKeyWhenCacheDisabled()
363363
$this->assertFalse( $Controller->testHasViewCacheByKey( 'product', $CacheKeyData ) );
364364
}
365365

366+
public function testRenderWithCacheKeyRespectsDefaultSettings()
367+
{
368+
// Setup cache that is enabled by default
369+
$Storage = new FileCacheStorage( $this->TempCacheDir );
370+
$ViewCache = new ViewCache( $Storage, true, 3600 ); // Cache enabled
371+
Registry::getInstance()->set( 'ViewCache', $ViewCache );
372+
373+
// Create controller
374+
$Controller = new CacheKeyTestController( new Router() );
375+
376+
$ViewData = ['product_name' => 'Default Test', 'price' => 75];
377+
$CacheKeyData = ['id' => 999];
378+
379+
// Render with null (should use default - enabled)
380+
$Result1 = $Controller->renderHtmlWithCacheKey(
381+
HttpResponseStatus::OK,
382+
$ViewData,
383+
$CacheKeyData,
384+
'product',
385+
'default',
386+
null // Use default settings
387+
);
388+
389+
$this->assertStringContainsString( 'Default Test', $Result1 );
390+
391+
// Should be cached because default is enabled
392+
$this->assertTrue( $Controller->testHasViewCacheByKey( 'product', $CacheKeyData ) );
393+
394+
// Now render again with empty data - should use cache
395+
$Result2 = $Controller->renderHtmlWithCacheKey(
396+
HttpResponseStatus::OK,
397+
[], // Empty data
398+
$CacheKeyData,
399+
'product',
400+
'default',
401+
null // Use default settings
402+
);
403+
404+
$this->assertEquals( $Result1, $Result2 );
405+
}
406+
407+
public function testRenderWithCacheKeyRespectsDefaultDisabled()
408+
{
409+
// Setup cache that is disabled by default
410+
$Storage = new FileCacheStorage( $this->TempCacheDir );
411+
$ViewCache = new ViewCache( $Storage, false, 3600 ); // Cache disabled
412+
Registry::getInstance()->set( 'ViewCache', $ViewCache );
413+
414+
// Create controller
415+
$Controller = new CacheKeyTestController( new Router() );
416+
417+
$ViewData = ['product_name' => 'Disabled Test', 'price' => 25];
418+
$CacheKeyData = ['id' => 888];
419+
420+
// Render with null (should use default - disabled)
421+
$Result = $Controller->renderHtmlWithCacheKey(
422+
HttpResponseStatus::OK,
423+
$ViewData,
424+
$CacheKeyData,
425+
'product',
426+
'default',
427+
null // Use default settings
428+
);
429+
430+
$this->assertStringContainsString( 'Disabled Test', $Result );
431+
432+
// Should NOT be cached because default is disabled
433+
$this->assertFalse( $Controller->testHasViewCacheByKey( 'product', $CacheKeyData ) );
434+
}
435+
436+
public function testRenderWithCacheKeyOverridesDefault()
437+
{
438+
// Setup cache that is disabled by default
439+
$Storage = new FileCacheStorage( $this->TempCacheDir );
440+
$ViewCache = new ViewCache( $Storage, false, 3600 ); // Cache disabled by default
441+
Registry::getInstance()->set( 'ViewCache', $ViewCache );
442+
443+
// Create controller
444+
$Controller = new CacheKeyTestController( new Router() );
445+
446+
$ViewData = ['product_name' => 'Override Test', 'price' => 100];
447+
$CacheKeyData = ['id' => 777];
448+
449+
// Render with true (should override default and enable cache)
450+
$Result = $Controller->renderHtmlWithCacheKey(
451+
HttpResponseStatus::OK,
452+
$ViewData,
453+
$CacheKeyData,
454+
'product',
455+
'default',
456+
true // Override default - force enable
457+
);
458+
459+
$this->assertStringContainsString( 'Override Test', $Result );
460+
461+
// The cache was stored when we forced it with true
462+
// To verify, enable the cache globally and check if the content exists
463+
$ViewCache->setEnabled( true );
464+
$CacheKey = $ViewCache->generateKey( 'CacheKeyTestController', 'product', $CacheKeyData );
465+
$this->assertTrue( $ViewCache->exists( $CacheKey ), 'Cache should exist after forcing with true' );
466+
467+
// Now test the opposite - cache enabled by default, but override to disable
468+
$ViewData2 = ['product_name' => 'Override2', 'price' => 200];
469+
$CacheKeyData2 = ['id' => 666];
470+
471+
$Result2 = $Controller->renderHtmlWithCacheKey(
472+
HttpResponseStatus::OK,
473+
$ViewData2,
474+
$CacheKeyData2,
475+
'product',
476+
'default',
477+
false // Override default - force disable
478+
);
479+
480+
$this->assertStringContainsString( 'Override2', $Result2 );
481+
482+
// Should NOT be cached even though default is enabled
483+
$CacheKey2 = $ViewCache->generateKey( 'CacheKeyTestController', 'product', $CacheKeyData2 );
484+
$this->assertFalse( $ViewCache->exists( $CacheKey2 ), 'Cache should not exist after forcing with false' );
485+
}
486+
366487
private function deleteDirectory( string $Dir ): void
367488
{
368489
if( !is_dir( $Dir ) )

versionlog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.6.45 2025-08-12
2+
* Fixed an issue with the cache override.
3+
14
## 0.6.44 2025-08-12
25
* Improved caching to support getting/setting cache data by a separate data key. This allows for avoiding expensive api calls in the controller when possible.
36

0 commit comments

Comments
 (0)