Output Caching
To output a content item in Speck, you normally use the cf_spContent tag. This tag has to retrieve the content from the database and then output it using the specified method. This isn't really something that needs to be done for each request unless content is constantly changing.
The cf_spCacheThis tag allows you to cache blocks of generated output in memory. This results in a minor performance penalty the first time the output is generated, but from then on the output can be obtained from the cache in memory rather than having to call cf_spContent again. Response times increase massively as a result of caching and it also allows your application to handle much heavier loads.
The home page and news page templates, updated to take advantage of output caching, are listed below.
index.cfm
<cfoutput>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<h2>Hello from Speck...</h2>
</cfoutput>
<cf_spCacheThis cacheName="home_blurb">
<cf_spContent type="Blurb" label="home">
</cf_spCacheThis>
<cfoutput>
</body>
</html>
</cfoutput>
news.cfm
<cfoutput>
<html>
<head>
<title>Speck News</title>
</head>
<body>
<h2>Speck News</h2>
</cfoutput>
<cfif isDefined("url.spId")>
<cf_spCacheThis cacheName="news_#replace(url.spId,"-","","all")#">
<cf_spContent type="Article" id="#url.spId#">
</cf_spCacheThis>
<cfelse>
<cf_spCacheThis cacheName="news_summaries">
<cf_spContent type="Article" keywords="news" method="summary">
</cf_spCacheThis>
</cfif>
<cfoutput>
</body>
</html>
</cfoutput>
If you update your index.cfm and news.cfm files to use the cf_spCacheThis tag as shown, you'll notice a significant increase in performance one the caches have been built.
If you don't notice any performance improvement, make sure both the admin links and cache info boxes in the toolbar are unchecked. Speck automatically disables caching while the admin links are on to avoid caching the admin links themselves and to make sure that users see the actual content while editing, not a cached version.
The cf_spCacheThis tag caches everything between the opening and closing tag, and every opening tag must have a matching closing tag. It has one required attribute, cacheName. The cache name must be a valid ColdFusion variable name and should uniquely identify the generated content.
Our new news page provides a good example of where a dynamically generated cache name is required. The code builds a cache name based on the id of the article. If we didn't do this, only one story would be cached at a time and this story would always appear, regardless of the value of the spId url parameter!
Most of the time, Speck takes care of flushing the relevant caches automatically when content is added or modified, but you can also use the optional cacheExpires attribute to set a cache to expire after a number of minutes or at a certain datetime.