Speeds up page rendering when dynamic content does not have to be retrieved each time a user accesses the page. To accomplish this, cfcache
creates temporary files that contain the static HTML returned from a ColdFusion page.
You can use cfcache
for simple URLs and URLs that contain URL parameters.
<cfcache action = "cache" or "flush" or "clientCache" or "optimal" username = "username" password = "password" protocol = "protocol_name" timeout = "#DateAdd(datepart, number, date)#" directory = "directory_name_for_map_file" cacheDirectory = "directory_name_for_cached_pages" expireURL = "wildcarded_URL_reference" port = "port_number">
cfflush,
cfheader,
cfhtmlhead,
cfsetting,
cfsilent
To enable the simplest form of caching, you just code cfcache
at the top of a page.
With the action
attribute, you can specify server-side caching, browser caching, or a combination of the two. The advantage of browser caching is that it takes no ColdFusion resources; the browser stores pages in its own cache, improving performance. The advantage of using a combination of the two forms of caching is that it optimizes performance; if the browser cache times out, the server can retrieve cached data from its own cache.
In addition to storing cached files, cfcache
uses a mapping file, cfcache.map
, to control caching. It uses a format similar to a Windows INI file. The mapping of a URL with parameters is stored as follows. Assume a directory c:\InetPub\wwwroot\dir1
that has a CFM file called foo.cfm
, which can be invoked with or without URL parameters. The cfcache.map file entries for foo.cfm looks like this:
[foo.cfm] Mapping = C:\InetPub\wwwroot\dir1\CFCBD.tmp SourceTimeStamp = 08/31/1999 08:59:04 AM [foo.cfm?x = 5] Mapping = C:\InetPub\wwwroot\dir1\CFCBE.tmp SourceTimeStamp = 08/31/1999 08:59:04 AM [foo.cfm?x = 9] Mapping = C:\InetPub\wwwroot\dir1\CFCBF.tmp SourceTimeStamp = 08/31/1999 08:59:04 AM
The cfcache.map
file in a directory stores mappings for that directory only. If the timestamp of the underlying page changes, ColdFusion updates the cache file for that URL. ColdFusion uses the SourceTimeStamp field to determine whether the currently cached file is up to date.
You can refresh the cache in the following ways:
timeout
attribute ColdFusion tests the timestamp of the cached file against the timeout
attribute If the cached file's timestamp is older than timeout
, it deletes the old file and creates a new one. You can use fixed dates if necessary, but it's better to use relative dates. This works for seconds, hours, days, weeks, years, etc.
action = "flush"
You use action = "flush"
to force the clean up of cached files. It can take two attributes, directory
and expireURL
.cffile
), delete .tmp
files. This is not recommended.
Note the following regarding cfcache
:
cfcache
requires that ColdFusion Server "simultaneous requests" be greater than 1. When a cache file is generated, the requested page requires two connections to satisfy the request. When a cached file is found, only one request is required.
cfcache
unless the application page explicitly turns it on. When generating a cached file, cfcache
uses cfsetting showDebugOutput = "No"
.cfcache
with the Secure Sockets Layer (SSL), specify protocol = "http://"
.<!--- This example produces as many cached files as there are possible URL parameter permutations. ---> <cfcache timeout = "#DateAdd("h", "-4", Now() )#"> <html> <head> <title>cfcache Example</title> </head> <body> <H1>cfcache Example</H1> <H3>This is a test of some simple output</H3> <cfparam name = "URL.x" default = "no URL parm passed" > <cfoutput>The value of URL.x = # URL.x #</cfoutput> </body> </html>