Fragment cache
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 |
|---|---|---|
|
|
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
-
{cache 'name', ttl=300} ... {/cache}- a named section; the name is its key within the application, so the same name is the same section on every page. -
{anyTag cache=3600}- the option form on any tag; on a hit the tag's own handler is skipped as well. It is keyed on the page, the tag as written and its evaluated parameters.
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 |
|---|---|---|
|
|
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
-
$padFragmentCache-'file'(default, underDATA/cache/fragments/),'apcu', orFALSEto render every time. -
padFragmentForget ( 'name' )drops a named section when what it shows has changed - one kept withvary=per value,padFragmentForget ( 'basket', $userId );padFragmentForget ()drops them all. - Closing pipes and end options -
{/cache | upper},toContent- run on a hit as on a fresh rendering.