# **Chapter 20: Output Cache — Serving Pages From cachelist.php**
> **Prerequisites:** Chapters 1-2 (URL → Gate flow)
> **Try it:** Tested code from gate `ex19cache`
> (`apps/examples/Ex19cache.gate.php`) + project `cachelist.php`.
> Open `http://localhost/SartajPhpDoc/ex19cache.html` and reload within 30s.
---
## **20.1 What the Output Cache Is**
SartajPhp can store the **rendered HTML of a GET URL** and replay it for the
TTL without running the Gate at all. The registry is one project file:
```php
<?php
// cachelist.php — loaded early by the engine (global/start.php)
addCacheList('ex19cache', 30); // cache this Gate's responses for 30 seconds
```
The demo Gate just proves the page is stale on purpose (tested):
```php
<?php
// apps/examples/Ex19cache.gate.php
class Ex19cache extends BasicGate
{
public $generatedAt = '';
public function page_new()
{
$this->generatedAt = date('c') . ' (PID ' . getmypid() . ')';
$this->debug->println('ex19cache page_new generatedAt=' . $this->generatedAt);
$this->setFrontFile($this->frtMain);
}
/** Bypass demo: force fresh by using an uncached event name */
public function page_event_fresh($evtp)
{
$this->generatedAt = date('c') . ' FRESH event (not in cachelist as cep)';
$this->setFrontFile($this->frtMain);
}
}
```
Reload `ex19cache.html` repeatedly within 30 seconds → the timestamp (and PID)
**do not change** — the Gate never ran. After 30s → new stamp.
## **20.2 addCacheList — Four Granularities**
```php
addCacheList($url, $sec = 0, $type = "Gate");
```
| `$type` | Matches | Example |
|---------|---------|---------|
| `"Gate"` (default) | Gate name, **all events** | `addCacheList('index', 100);` |
| `"ce"` | Gate + event | `addCacheList('blog-view', 1800, "ce");` |
| `"cep"` | Gate + event + event parameter | `addCacheList('shop-product-shirt', 3600, "cep");` |
| `"e"` | Event name on **any** Gate | `addCacheList('info', 3600, "e");` |
`$sec`: TTL in seconds; `-1` = never expire (until cache cleared).
Pick the **narrowest** type that still hits your traffic: caching a whole
Gate (`"Gate"`) also caches its edit forms and AJAX events — almost never what
you want on a member area. Public content pages → `"ce"` or `"cep"`.
## **20.3 What Is Never Cached**
1. **POST requests** — form submits always run the Gate (the demo front notes
this; `page_submit` can't be silently skipped)
2. **URLs not in `cachelist.php`** — e.g. `ex19cache-fresh.html` is not
registered as `cep`, so it always runs (tested bypass)
3. **Sessions/permissions are not per-visitor** — a cached page is one HTML
blob for everyone; never cache pages whose HTML differs per user role
(Chapters 14/18 render role-dependent UI → keep them out of the cache list)
## **20.4 Choosing a Caching Strategy**
```mermaid
flowchart TD
Q{Public GET page?} -->|no| N[Do not cache]
Q -->|yes| R{Same HTML for all visitors?}
R -->|no| N
R -->|yes| T{Changes how often?}
T -->|rarely| A[addCacheList cep/ce, long TTL or -1]
T -->|minutes| B[addCacheList ce, 30-300s]
T -->|per request| N
```
Practical notes:
- **Under a caching server** (Apache/nginx serving the cached file) the stale
timestamp demo is exactly what production traffic experiences
- **Data freshness**: cache the *list* page 60s, keep the *edit* events
uncached — users must always see their own latest save
- **Login areas**: cache nothing per-Guest; cache only shared marketing pages
- Related but different: `DbEngine::fetchQuery($sql, $ttl, $filename)` caches
**query results** (Chapter 12's parent class), and `insertCache/updateCache`
maintain cache-backed datasets — page cache vs data cache, choose per need
## **20.5 Verify It Yourself**
Against `http://localhost/SartajPhpDoc/`:
1. Confirm `cachelist.php` contains `addCacheList('ex19cache', 30);`
2. Open `ex19cache.html` → note timestamp + PID
3. Reload 5× within 30s → identical stamp (Gate skipped; console shows no new
`println` line)
4. Wait past 30s → reload → new stamp
5. `ex19cache-fresh.html` → always fresh (not registered)
---
**Previous:** [Chapter 19 — Debugging and Error Tracing](chapter19.md) ·
**Next:** [Chapter 21 — ConsoleGate: CLI Shell Commands](chapter21.md)