Contents |
The CFCACHECONTENT tag allows you to cache specific blocks of HTML/CFML content to memory or a database table.
<cfcachecontent action="action to perform"
cachename="cache name"
group="group name"
cachedwithin="timespan until cache expires">
... content to cache ...
</cfcachecontent>
| Attribute | Req/Opt | Default | Description |
|---|---|---|---|
| action | required | cache | The action to perform. Permitted values are:
|
| cachename | required when action=cache | N/A | The name to assign to the cache being created. |
| group | optional | CGI.SERVER_NAME | An optional group name to assign to the cache. |
| cachedwithin | optional | N/A | The maximum time (specified using CreateTimeSpan() to pull from the cache before the cache is recreated. |
CFCACHECONTENT provides a simple yet powerful way to cache rendered page content so it does not need to be generated each time it is requested. CFCACHECONTENT is essentially a combination of CFSAVECONTENT and CFCACHE but allows for much more granular control over caching. In addition, the cached data can be stored in either the server's RAM or in a database table.
The CACHENAME and GROUP attributes provide granular control over caching data and clearing the cached data. The STATS action provides a simple way of seeing how effective the caching is by providing hit and miss statistics for the cache.
By default, cache data is stored in server RAM, which means the cache will not persist server restarts. By adding a datasource value for CFCACHECONTENT to the bluedragon.xml file, however, the cached data can be stored in a database:
<server>
<cfcachecontent>
<datasource>datasource_name</datasource>
<total>20</total>
</cfcachecontent>
</server>
Note that this setting is not currently accessible via the OpenBD administrator, but it will be added to a future version.
When the cache is stored to the database, a table called LRUCACHE is created if it doesn't already exist. The <total> node indicates the number of items that are stored in memory before the database is used. All cache operations including flushes affect the database cache as well.
Cache the output between the CFCACHECONTENT tags with a name of "mycache" in the group "mygroup" for 20 minutes:
<cfcachecontent action="cache" cachename="mycache" group="mygroup" cachedwithin="#CreateTimeSpan(0,0,20,0)#"> <cfoutput>#myCFC.getLotsOfData()#</cfoutput> </cfcachecontent>
Flush the cache named "mycache":
<cfcachecontent action="flush" cachename="mycache">
Dump the stats for the entire cache:
<cfcachecontent action="stats"> <cfdump var="#CFCACHECONTENT#">