silverstripe-framework/docs/en/02_Developer_Guides/01_Templates/07_Caching.md
Aaron Carlino 6888901468
NEW: Update docs to be compliant with Gatsby site (#9314)
* First cut

* Temporarily disable composer.json for netlify build

* POC

* New recursive directory query, various refinements

* Fix flexbox

* new styled components plugin

* Apply frontmatter delimiters

* Mobile styles, animation

* Search

* Redesign, clean up

* Nuke the cache, try again

* fix file casing

* Remove production env file

* ID headers

* Move app to new repo

* Add frontmatter universally

* Hide children changelogs

* Add how to title

* New callout tags

* Revert inline code block change

* Replace note callouts

* Fix icons

* Repalce images

* Fix icon

* Fix image links

* Use proper SQL icon
2019-11-18 17:58:33 +13:00

1.1 KiB

title summary icon
Caching Reduce rendering time with cached templates and understand the limitations of the ViewableData object caching. rocket

Caching

Object caching

All functions that provide data to templates must have no side effects, as the value is cached after first access. For example, this controller method will not behave as you might imagine.

private $counter = 0;

public function Counter() 
{
    $this->counter += 1;

    return $this->counter;
}
$Counter, $Counter, $Counter

// returns 1, 1, 1

When we render $Counter to the template we would expect the value to increase and output 1, 2, 3. However, as $Counter is cached at the first access, the value of 1 is saved.

Partial caching

Partial caching is a feature that allows the caching of just a portion of a page. Instead of fetching the required data from the database to display, the contents of the area are fetched from a cache backend.

<% cached 'MyCachedContent', LastEdited %>
    $Title
<% end_cached %>