> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/packages/resourcecache/overview.md).

# Overview

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

```php
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/packages/resourcecache/overview.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
