3.1.5.3. Funktions for caching


Caching allows developers to increase the speed of heavy webshops or websites. Read about the 3 features below that can be used to cache Smarty items with, for example. a menu.

 

CacheIntent

The CacheIntent feature is for extracting and creating cache elements. In other words, an initialization of the cache that can be used for extracting via the cache function. 

CacheIntent accepts the following parameters:

Navn Default Beskrivelse Påkrævet
name '' Name for cache recognition Yes
cache_site true Cache is unique to each site No
cache_iso true Cache is unique to each language layer No
cache_uri true Cache is unique to each REQUEST_URI No
cache_currency true Cache is unique to each currency, if available No
ttl 3600 Cache life (Time To Live) in seconds No
assign '' Name of new variable containing object instance Yes

 

 

Cache

The cache function is for saving caching data. The data saved with cache is saved in the cache instance created with cacheIntent.

Cache accepts the following parameters:

Navn Default Beskrivelse Påkrævet
content '' Content to cache Yes
cacheIntent '' Name of cacheIntent to use Yes

 

 

ClearCache

The ClearCache feature is used to delete or clear a particular cache. Eg. If a user logs out or for other reasons, you need to delete the content. Eg. in footer, where you remove the newsletter notification for a user once they have been created. There may be many different reasons for deleting a cache.

Cache accepts the following parameters:

Navn Default Beskrivelse Påkrævet
name '' Name of cacheIntent to be deleted Yes

 

 

About the use of caching

Caching is ideal for components or template files that are highly resource heavy. Or to cache components and templates that are repeated many places across the solution, for example a menu or footer.

The most important part of the caching functions is that the naming of the cache is unique, because the name is used to retrieve the cache at each load. Since the name is unique it can also be used to make the cache unique on more levels then the caching paramaters allow. In the menu example below we make the name unique for each page id, by truncating the page id with our cache name "menu_", doing this, makes the cache unique for each page in the navigation. At the same time we use false on the URI parameter for the cacheIntent. We do this to avoid issues with the webshop module, that has the webshop page on one page id, and therefor creates many different URIs for the same page id, without actually changing the navigation. This is a example of dynamic naming and clever usage of the cacheIntent parameters.

The example below shows how to use caching features to cache a menu. So that the caching of the menu works, even though the language changes, even by navigation (active / active page selection) and by site change in case you use templates across different sites.

Note: Caching creates cache files on the solution, which is calculated in the space usage.

 

Example 1: caching menu, a template file

The example is developed on the Rooty Template, in the partials/top.tpl file.

We form the cache from several levels, described here:

  • Language layers - menu items change with the language, therefore we have to a cache per language layer.
  • Site - Different sites may have different menu items, and therefore a cache per site.
  • Page id - To cache menu per page so we can show if the selected page is active. You can also cache on URL, but it will save far more cache files.
  • User logged in - The menu changes when logged in, for example under the menu item My account.

 

*CODE* *SNIPPIT*
{* We create a unique name for the cache, so we have a cache for each case *}
{$menuCacheName = "menu_"}
{$menuCacheName = $menuCacheName|cat:"{($user) ? '1' : '0'}_"}
{$menuCacheName = $menuCacheName|cat:$page.id}

{* Notify framework that you intent to cache a piece of code *}
{cacheIntent name=$menuCacheName cache_uri=false assign=cache}

{* Check intent object for existing cache *}
{if $cache.content}
    {$cache.content}

{* If empty the cache has not been set or has expired, so prepare a new cache *}
{else}

    {menu assign=primaryMenu static=$static}

    {* Assign the included file to a variable *}
    {include
        file='modules/widgets/menu/menu.tpl'
        items=$primaryMenu
        classes='nav nav-default'
        assign=menuContent
    }

    {* Save the captured file with the cache intent object, and echo the file content itself to the page *}
    {cache content=$menuContent cacheIntent=$cache}
{/if}*CODE*

 

 

Example 2: Footer caching, a section or part of a template

The example is developed on the Rooty Template, in the partials/bottom.tpl file.

We remove some of the caching levels as the section is not unique to, for example currency or user, the levels we cache on are therefore

  • Language layers - menu items change with the language, therefore we have to a cache per language layer.
  • Site - Different sites may have different menu items, and therefore a cache per site.

To save template data, we use {capture}, a feature built into Smarty to save content into a variable.{capture} is used because we want to save parts of a template into a variable, {capture} takes the output from Smarty, hence pure HTML and saves it as a string. In our case in the variable called $footerCache.

*CODE* *SNIPPIT*
{* Notify framework that you intent to cache a piece of code *}
{cacheIntent name=footer assign=cache cache_uri=false cache_currency=false cache_user=false}

{* Check intent object for existing cache *}
{if $cache.content}
    {$cache.content}

{* If empty the cache has not been set or has expired, so prepare a new cache *}
{else}
    {* First capture (equal to ob_start) the region that is to be cached *}
    {capture assign=footerCache}
    


    {/capture}

    {* Save the captured region with the cache intent object, and echo the region itself to the page *}
    {cache content=$footerCache cacheIntent=$cache}
{/if} *CODE*