ResourceCache

The ResourceCache package stores rendered HTML responses on disk so repeated requests can skip view rendering work.

Use it when you want simple full-page caching for HTML endpoints, especially pages that are expensive to render and benefit from a short-lived per-visitor cache.

What the package provides

The package is centered on one service:

  • Quantum\ResourceCache\ViewCache

It can:

  • read a cached HTML response for a request key
  • store rendered HTML to disk
  • expire entries automatically by TTL
  • optionally minify HTML before saving

Basic flow

use Quantum\ResourceCache\ViewCache;

$cache = new ViewCache();
$cache->setup();

if ($response = $cache->getCachedResponse(request()->getPath())) {
    return $response;
}

$html = RendererFactory::get()->render('home/index', ['posts' => $posts]);

$cache->set(request()->getPath(), $html);

return response()->html($html);

getCachedResponse() returns a ready-made HTML Response when a valid cached entry exists. Otherwise it returns null.

Configuration

The package reads two config areas:

  • resource_cache controls whether cached responses are used
  • view_cache controls storage details

On setup(), the package lazily imports config/view_cache.php if it has not been loaded yet.

The global resource_cache switch is read when you create the ViewCache instance. In most applications that config is already available during normal bootstrap. If you construct the cache earlier, create it after config bootstrap or call enableCaching(true) for that instance.

Supported view_cache options used by this package:

  • cache_dir - base cache directory relative to base_dir(); defaults to cache
  • ttl - entry lifetime in seconds; defaults to 300
  • minify - whether HTML is minified before saving; defaults to false

Cache layout

Cache files are stored under:

  • base_dir()/<cache_dir>/views for shared requests
  • base_dir()/<cache_dir>/views/<module> when the current request has a module

The module name is lowercased for the directory name.

This means identical cache keys are separated by current module automatically.

Important contracts

  • Cache file names are derived from the cache key plus the current session ID.
  • Cached HTML is therefore visitor-scoped by default.
  • Expired files are removed the first time the package checks them after their TTL passes.
  • getCachedResponse() serves cached output when caching is enabled for the current ViewCache instance.
  • Direct methods such as set(), get(), and delete() keep working regardless of that read toggle.

Common pitfalls

Always call setup() before using the cache

setup() loads config, decides the cache directory, and creates that directory when missing.

If you skip it, cache file paths are not initialized correctly.

Plan keys around visitor-scoped cache files

The package includes session()->getId() in the cache file name.

That makes the built-in cache a good fit for per-visitor HTML. For shared page caching across all users, use a different cache layer.

Enable minification when the HTML minifier package is available

When minification is enabled, set() expects the voku\helper\HtmlMin package to be installed. If it is missing, the write raises a resource-cache exception.