Components can be made extremely fast by caching their output. When implementing body() or header(), you must consider that the cache key depends on request parameters and configuration settings.
Caching Logic
- Identify Dependencies: Determine which parameters (e.g., prefixed with
f for filters, l for lists, or d for details) and which configuration keys (e.g., client/html/catalog/detail) affect the output. - Check Cache: Use
$this->cached() to attempt to retrieve existing content. - Generate Content: If not cached, render the template using the view.
- Store Cache: Use
$this->cache() to store the generated HTML.
Handling Non-Cachable Content (Sessions/Cookies)
If a component depends on user sessions or cookies, the entire output cannot be cached. However, you can still cache the bulk of the component and use the modify() method to allow subclients to replace specific sections of the cached HTML with dynamic, non-cached content.
public function body( string $uid = '' ) : string
{
$view = $this->view();
$config = $this->context()->config();
$params = ['d_prodid', 'd_name'];
$confkey = 'client/html/catalog/detail';
if( $html = $this->cached( 'body', $uid, $params, $confkey ) ) {
return $this->modify( $html, $uid );
}
$template = $config->get( 'client/html/catalog/detail/template-body', 'catalog/detail/body' );
$view = $this->view = $this->view ?? $this->object()->data( $view, $this->tags, $this->expire );
$html = $this->modify( $view->render( $template ), $uid );
return $this->cache( 'body', $uid, $params, $confkey, $html, $this->tags, $this->expire );
}