@@ -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 }
0 commit comments