Developer's Manual  Using Cache

Using Cache

When a blockāĄ™s output stays permanent and does not depend on user logged in or on other conditions, you can use SiteSupra cache for increasing web site performance. There are two cache modes available in SiteSupra: the first level cache when a blockāĄ™s output is fully cached by SiteSupra and the second level cache when a developer can decide which data to cache.

The cached blocksāĄ™ output is stored in the cache field of the content table. Thus, there is no additional query executed to get the cache content. When SiteSupra executes a page for the first time, it collects blocksāĄ™ output into the array and saves the array into the cache field of the page. Next time when a site visitor requests the page, SiteSupra will not include and execute blocks, which are using first level cache. Instead of executing the blocks, SiteSupra will insert blocksāĄ™ output taken from the cache into the page. If a block uses second level cache, SiteSupra delivers cache data saved from last time the block was executed.

Information in the cache field expires with every Approve operation.

When using SiteSupra cache do not forget to turn on caching in .htsupra file and select correct cache mode for your blocks.

Using second level cache

The example shows simple block that shows news titles. The block shows the titles in small portions. Because of the fact, the block may show different page titles with the next request its output could not be cached. However, using the second level cache we can store the query result and do not hit the database again next time.

<?
$itemsPerPage
= 2;

// checking whether data was saved
// into the cache already
if(!$suCACHE['data'])
{
  
$query = "SELECT id, date, title, short
            FROM "
. suPAGES . "
            WHERE active = 1 AND path LIKE '/news/%'
            ORDER BY date DESC"
;

  
// creating array and storing query result
  
$suCACHE['data'] = array();
  
$suCACHE['data']['news'] = $data = $suSQL->queryData($query);
  
$suCACHE['save'] = 1;
}
else
  
// loading data from the cache into array
  
$data = $suCACHE['data']['news'];    

$page = getGetVar('page', 1);
$news = array_slice($data, ($page - 1) * $itemsPerPage, $itemsPerPage);
$totalPages = ceil(count($data) / $itemsPerPage);
$path = suBASE . '/' . suLANG . $suDOC['path'] . '?page=';

// sending data into template engine
$suTMPL->set('news', $news);
$suTMPL->set('prev', '<a href="' . $path . ($page == 1 ? 1 : $page - 1) . '">previous page</a>');
$suTMPL->set('next', '<a href="' . $path . ($page == $totalPages ? $totalPages : $page + 1) . '">next page</a>');
$suTMPL->exec();
?>

block.tmpl file:


<table width="100%" border="0" cellpadding="2" cellspacing="0">
    <
loop:news>
    <
tr>
        <
td>{$news.title}</td>
        <
td>{$news.date}</td>
    </
tr>
    <
tr>
        <
td colspan="2">{$news.short}</td>
    </
tr>
    </
loop:news>
    <
tr>
        <
td>{$prev}</td>
        <
td>{$next}</td>
    </
tr>
</
table>
Please login to add comments.