The page cache answers a whole request from a store. The fragment cache keeps a section of a page rendered, while the page around it stays dynamic: on a hit nothing inside the section runs - no tag, no query - and the stored rendering takes its place.

PHP
fragments/cache_1.php
HTML
fragments/cache_1.pad
Result
<?php

// Start the example from an empty entry, so it shows a miss and then a hit.

padFragmentForget ( 'manual-cache' );

$built = 0;

?>
{cache 'manual-cache', ttl=60}
{set $built = $built + 1}
<p>Rendered while building: built {$built} time(s)</p>
{/cache}

{cache 'manual-cache', ttl=60}
{set $built = $built + 1}
<p>Rendered while building: built {$built} time(s)</p>
{/cache}

<p>The section was built {$built} time: the second one came from the cache.</p>

Rendered while building: built 1 time(s)

Rendered while building: built 1 time(s)

The section was built 1 time: the second one came from the cache.

Two forms

What the key depends on

Everything that changes the rendering must be in the key. Add it with vary: {cache 'basket', vary=$userId} keeps one copy per user. PHP that runs before the template - the page's own .php - runs on a hit too; the saving is the work done inside the section.

Parts that stay live

A {nocache}...{/nocache} part of a section renders on every request, the hit included: the section is stored with the part as its source, and the part renders again in the stored rendering. Inside a loop of the section it gets the fields of its row back.

PHP
fragments/cache_2.php
HTML
fragments/cache_2.pad
Result
<?php

// Start the example from an empty entry, so it shows a miss and then a hit.

padFragmentForget ( 'manual-nocache' );

$built = 0;
$visit = 1;

?>
{cache 'manual-nocache', ttl=60}
{set $built = $built + 1}
<p>Cached: built {$built} time(s), {nocache}live: visit {$visit}{/nocache}</p>
{/cache}

{set $visit = 2}

{cache 'manual-nocache', ttl=60}
{set $built = $built + 1}
<p>Cached: built {$built} time(s), {nocache}live: visit {$visit}{/nocache}</p>
{/cache}

Cached: built 1 time(s), live: visit 1

Cached: built 1 time(s), live: visit 2

The same works for the page cache: a page it keeps renders its {nocache} parts on every hit. No PHP runs on such a hit - not the page's, nor an _inits.php - so there a part sees the request and session values, _lib and the application's tags.

Settings and housekeeping