cfcache

Description

Stores a copy of a page on the server and/or client computer, to improve page rendering performance. To do this, the tag creates temporary files that contain the static HTML returned from a ColdFusion page.

Categories

Related

History

ColdFusion 9: Added support for the following features: Caching in memory. Memory is now the default cache location. Caching page fragments. Caching specific objects, including the ability to put, get, and flush cached objects. Setting cache dependencies. Setting an idle timeout. Getting metadata about cached objects. The ability to strip white space from cached page fragments. The ability to throw an exception if an error occurs when flushing a cached object. Added get and put values of the action attribute. These values support caching of objects. Added dependsOn, id, idleTime, key, metadata, name, stripWhiteSpace, throwOnError, useCache, usequerystring, and value attributes. ColdFusion MX: Deprecated the cachedirectory and timeout attributes. They might not work, and might cause an error, in later releases. Added the timespan attribute. Changed how pages are cached: the default action attribute value, cache, caches a page on the server and the client. (In earlier releases, this option cached a page only on the server.) Changed the source of the protocol and port values: the default protocol and port values are now taken from the current page URL. (In earlier releases, they were "http" and "80", respectively.) Changed how session state is handled when caching a page: this tag can cache pages that depend on session state, including pages that are secured with a ColdFusion login. (In earlier releases, the session state was cleared when caching the page, causing authentication to be lost.) Changed how files are cached: this tag uses a hash() of the URL for the filename to cache a file. (In earlier releases, ColdFusion used the cfcache.map file.)

Syntax

<cfcache  
    action = "action" 
    dependsOn = "variable name list" 
    directory = "directory path" 
    expireURL = "wildcarded URL reference" 
    id = "object identifier" 
    idleTime = "decimal number of days" 
    key = "key value" 
    metadata = "variable name" 
    name = "variable name" 
    password = "password" 
    port = "port number" 
    protocol = "http://|https://" 
    stripWhiteSpace = "false|true" 
    throwOnError = "false|true" 
    timespan = "decimal number of days"> 
    useCache = "true|false" 
    usequerystring = "false|true" 
    username = "username" 
    value = "value"> 
 
    The page fragment to be cached, if any. 
 
</cfcache>
Note: You can specify this tag’s attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag’s attribute names as structure keys.

Attributes

AttributeDescriptionRequiredDefault
actionAll
dependsOncacheserverCacheoptimal
directorycache,serverCache,clientCache,optimal, flush, put
expireURLflush
idflush, get, put
idleTimecache,serverCache,clientCache,optimal, flush, put
key 
metadataget
nameget
passwordcache,serverCache,clientCache,optimal, flush
portcache,serverCache,clientCache,optimal, flush
protocolcache,serverCache,clientCache,optimal, flush
stripWhiteSpacecache,serverCache,optimal
throwOnErrorflush with id attribute
timespancache,serverCache,clientCache,optimal, flush, put
useCachecache,serverCache,optimal,
usequerystring 
usernamecache,serverCache,clientCache,optimal, flush
valueput

Usage

ColdFusion 9 additions
The following is new information applies to features added for ColdFusion 9. For additional information, see the History section and the Attributes table.
Page fragments: To cache a page fragment, put the fragment in the body of the tag, between the begin tag and the end tag. Do not use a tag body to cache full pages or objects.
flush: The flush action can have two formats: One uses the ExpireURL attribute to specify the page to flush, the other uses the id attribute to specify the object to flush. When you flush an object, ColdFusion ignores errors by default. If you specify a throwOnError attribute with a true value, the action throws the errors, and you can use a catch block to handle them. This is useful to determine if you use invalid cache ID values.
User-defined cache: To create user-defined cache,
Add the following snippet to the ehcache.xml (in the CF_root\lib\):
<cache name="cf9" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="86400" timeToLiveSeconds="86400" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="true" diskExpiryThreadIntervalSeconds="3600" memoryStoreEvictionPolicy="LRU"/> To reference the user-defined cache, use the key attribute as follows:
<cfcache key="cf9" timespan=#createtimespan(0,0,1,0)# > <cfoutput>#now()#</cfoutput> <cfcache>
By default, caching is memory-based and not disk-based. For each application, the default setting is 10000 object caches and 10000 template caches. It is important to note the limit imposed on the number of objects/templates that can be cached.
Diskoverflow for caching by default is false. To enable disk caching, set overflowTodisk as true in the ehcache.xml. To make the cached data available on server restart, set diskPersistent to true.
For further details of the properties in the ehcache.xml, refer to the documentation available at the following URL:
http://ehcache.org/
From ColdFusion 8 and earlier
The following remaining information for this tag also applied to previous releases.
Use this tag in pages whose content is not updated frequently. Taking this action can greatly improve the performance of your application.
The output of a cached page is stored in a file on the client browser and/or the ColdFusion server. Instead of regenerating and downloading the output of the page each time it is requested, ColdFusion uses the cached output. ColdFusion regenerates and downloads the page only when the cache is flushed, as specified by the timespan attribute, or by invoking cfcache action=flush.
To enable a simple form of caching, put a cfcache tag, specifying the timespan attribute, at the top of a page. Each time the specified time span passes, ColdFusion flushes (deletes) the copy of the page from the cache and caches a new copy for users to access.
You can specify client-side caching or a combination of client-side and server-side caching (the default), using the action attribute. The advantage of client-side caching is that it requires no ColdFusion resources; the browser stores pages in its own cache, improving performance. The advantage of combination caching is that it optimizes server performance; if the browser does not have a cache of the page, the server can get data from its own cache. (Adobe recommends that you use combination caching, and do not use server-side only caching.)
If a page contains personalized content, use the action = "clientcache" option to avoid the possibility of caching a personalized copy of a page for other users.
Debug settings have no effect on cfcache unless the application page enables debugging. When generating a cached file, cfcache uses cfsetting showDebugOutput = "no".
The cfcache tag evaluates each unique URL, including URL parameters, as a distinct page, for caching purposes. For example, the output of http://server/view.cfm?id=1 and the output of http://server/view.cfm?id=2 are cached separately.
The cfcache tag uses the cfhttp tag to get the contents of a page to cache. If there is an HTTP error accessing the page, the contents are not cached. If a ColdFusion error occurs, the error is cached.
For more information, see Caching ColdFusion pages that change infrequently in the Developing ColdFusion Applications.

Example

<!--- This example produces as many cached files as there are URL parameter permutations. 
You can see that the page is cached when the timestamp doesn't change.---> 
 
<cfcache 
    timespan="#createTimeSpan(0,0,10,0)#"> 
<body> 
 
<h3>This is a test of some simple output</h3> 
 
<cfoutput> 
    This page was generated at #now()#<br> 
</cfoutput> 
 
<cfparam name = "URL.x" default = "no URL parm passed"> 
<cfoutput>The value of URL.x = # URL.x #</cfoutput>