@@ -252,6 +252,211 @@ protected function getViewCache( string $Page, array $Data = [] ): ?string
252252 return $ ViewCache ->get ( $ CacheKey );
253253 }
254254
255+ /**
256+ * Check if view cache exists using only cache key data.
257+ * This allows checking cache without fetching full view data.
258+ *
259+ * @param string $Page The page/view name
260+ * @param array $CacheKeyData The minimal data that determines cache uniqueness
261+ * @return bool True if cache exists, false otherwise
262+ */
263+ protected function hasViewCacheByKey ( string $ Page , array $ CacheKeyData = [] ): bool
264+ {
265+ $ ViewCache = $ this ->initializeViewCache ();
266+
267+ if ( !$ ViewCache || !$ ViewCache ->isEnabled () )
268+ {
269+ return false ;
270+ }
271+
272+ $ CacheKey = $ ViewCache ->generateKey (
273+ $ this ->getControllerName (),
274+ $ Page ,
275+ $ CacheKeyData
276+ );
277+
278+ return $ ViewCache ->exists ( $ CacheKey );
279+ }
280+
281+ /**
282+ * Get cached view content using only cache key data.
283+ * This allows retrieving cache without fetching full view data.
284+ *
285+ * @param string $Page The page/view name
286+ * @param array $CacheKeyData The minimal data that determines cache uniqueness
287+ * @return string|null The cached content or null if not found
288+ */
289+ protected function getViewCacheByKey ( string $ Page , array $ CacheKeyData = [] ): ?string
290+ {
291+ $ ViewCache = $ this ->initializeViewCache ();
292+
293+ if ( !$ ViewCache || !$ ViewCache ->isEnabled () )
294+ {
295+ return null ;
296+ }
297+
298+ $ CacheKey = $ ViewCache ->generateKey (
299+ $ this ->getControllerName (),
300+ $ Page ,
301+ $ CacheKeyData
302+ );
303+
304+ return $ ViewCache ->get ( $ CacheKey );
305+ }
306+
307+ /**
308+ * Render HTML with separate cache key data.
309+ * Allows checking/using cache without fetching full view data.
310+ *
311+ * @param HttpResponseStatus $ResponseCode HTTP response status
312+ * @param array $ViewData Full data for rendering (can be empty if using cache)
313+ * @param array $CacheKeyData Minimal data for cache key generation
314+ * @param string $Page Page template name
315+ * @param string $Layout Layout template name
316+ * @param bool|null $CacheEnabled Whether to enable caching
317+ * @return string Rendered HTML content
318+ * @throws \Neuron\Core\Exceptions\NotFound
319+ */
320+ public function renderHtmlWithCacheKey (
321+ HttpResponseStatus $ ResponseCode ,
322+ array $ ViewData = [],
323+ array $ CacheKeyData = [],
324+ string $ Page = "index " ,
325+ string $ Layout = "default " ,
326+ ?bool $ CacheEnabled = null
327+ ): string
328+ {
329+ @http_response_code ( $ ResponseCode ->value );
330+
331+ // If view data is empty and cache is enabled, try to get cached content
332+ if ( empty ( $ ViewData ) && $ CacheEnabled !== false )
333+ {
334+ $ CachedContent = $ this ->getViewCacheByKey ( $ Page , $ CacheKeyData );
335+ if ( $ CachedContent !== null )
336+ {
337+ return $ CachedContent ;
338+ }
339+ }
340+
341+ // Create view and set up for rendering
342+ $ View = new Html ()
343+ ->setController ( $ this ->getControllerName () )
344+ ->setLayout ( $ Layout )
345+ ->setPage ( $ Page )
346+ ->setCacheEnabled ( $ CacheEnabled );
347+
348+ // If we have view data, render normally
349+ if ( !empty ( $ ViewData ) )
350+ {
351+ $ RenderedContent = $ View ->render ( $ ViewData );
352+
353+ // Store in cache using cache key data
354+ if ( $ CacheEnabled === true )
355+ {
356+ $ ViewCache = $ this ->initializeViewCache ();
357+ if ( $ ViewCache && $ ViewCache ->isEnabled () )
358+ {
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 )
369+ {
370+ // Silently fail on cache write errors
371+ }
372+ }
373+ }
374+
375+ return $ RenderedContent ;
376+ }
377+
378+ // No view data and no cache - this shouldn't happen in normal use
379+ // but render with cache key data as fallback
380+ return $ View ->render ( $ CacheKeyData );
381+ }
382+
383+ /**
384+ * Render Markdown with separate cache key data.
385+ * Allows checking/using cache without fetching full view data.
386+ *
387+ * @param HttpResponseStatus $ResponseCode HTTP response status
388+ * @param array $ViewData Full data for rendering (can be empty if using cache)
389+ * @param array $CacheKeyData Minimal data for cache key generation
390+ * @param string $Page Page template name
391+ * @param string $Layout Layout template name
392+ * @param bool|null $CacheEnabled Whether to enable caching
393+ * @return string Rendered Markdown content
394+ * @throws \Neuron\Core\Exceptions\NotFound
395+ * @throws CommonMarkException
396+ */
397+ public function renderMarkdownWithCacheKey (
398+ HttpResponseStatus $ ResponseCode ,
399+ array $ ViewData = [],
400+ array $ CacheKeyData = [],
401+ string $ Page = "index " ,
402+ string $ Layout = "default " ,
403+ ?bool $ CacheEnabled = null
404+ ): string
405+ {
406+ @http_response_code ( $ ResponseCode ->value );
407+
408+ // If view data is empty and cache is enabled, try to get cached content
409+ if ( empty ( $ ViewData ) && $ CacheEnabled !== false )
410+ {
411+ $ CachedContent = $ this ->getViewCacheByKey ( $ Page , $ CacheKeyData );
412+ if ( $ CachedContent !== null )
413+ {
414+ return $ CachedContent ;
415+ }
416+ }
417+
418+ // Create view and set up for rendering
419+ $ View = new Markdown ()
420+ ->setController ( $ this ->getControllerName () )
421+ ->setLayout ( $ Layout )
422+ ->setPage ( $ Page )
423+ ->setCacheEnabled ( $ CacheEnabled );
424+
425+ // If we have view data, render normally
426+ if ( !empty ( $ ViewData ) )
427+ {
428+ $ RenderedContent = $ View ->render ( $ ViewData );
429+
430+ // Store in cache using cache key data
431+ if ( $ CacheEnabled === true )
432+ {
433+ $ ViewCache = $ this ->initializeViewCache ();
434+ if ( $ ViewCache && $ ViewCache ->isEnabled () )
435+ {
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 )
446+ {
447+ // Silently fail on cache write errors
448+ }
449+ }
450+ }
451+
452+ return $ RenderedContent ;
453+ }
454+
455+ // No view data and no cache - this shouldn't happen in normal use
456+ // but render with cache key data as fallback
457+ return $ View ->render ( $ CacheKeyData );
458+ }
459+
255460 /**
256461 * This method registers routes for any of the standard methods that
257462 * are currently implemented in the class.
0 commit comments