cf_cache

cf_cache

Version: 1.1 build 20050424
Requires: Adobe ColdFusion 5.0 or greater
Total size: 4.93 KB
Download time: 0.88 seconds at 56kbps
Edition: Freeware

Description

Cache the page content between the opening and closing tag to a static file on your server. The essential difference between this tag and the built-in cfcache tag is that this tag can cache sections of a page, rather than the entire page. Each cached item is addressed using a unique key, which may be a string or some other data structure (such as a struct). This tag will only improve performance where the sections being cached require significant server resources to generate (such as queries and calculations). Since this tag generates files on your server, it will not consume server memory.

Result

Renders content between tags from cache or generates fresh content as required.

Category

page processing

Parameters


Parameter Type Required? Default Description
directory string No getTempDirectory() Absolute path to cache folder. If convenient, you may set a default value for this parameter in request.eswsoftware.cache.defaults.directory .
flush boolean No false If yes, then delete all cached data. If convenient, you may set a default value for this parameter in request.eswsoftware.cache.defaults.flush .
key any Yes A unique string identifying this item in the cache. If you summply a complex variable such as a struct or array, then that will be transformed into a WDDX packet, and that packet will be used as the string. You could use a string such as 'navigation', or a UUID, or a string based on the filename, e.g. key='#getCurrentTemplatePath()#'. Or you could use a struct to hold several independent strings. For example, if a file generates different content for different users, then one key in the struct could represent the filename, while another represented the user's unique login name.
timespan numeric No 1 hour Timespan defining how long to cache the data. Use the built-in createTimeSpan() function, e.g. timespan="#createTimeSpan(0,1,0,0)#" . If convenient, you may set a default value for this parameter in request.eswsoftware.cache.defaults.timespan .
writeOnly boolean No false If yes, then write to the cache but do not read from the cache. This could be useful where a site administrator is logged in and editing a site. They want to see changes made instantly, and the changes they make should be reflected instantly for other users too. If convenient, you may set a default value for this parameter in request.eswsoftware.cache.defaults.writeOnly .

Custom tag source


 
<!---**
* cf_cache
* Cache the page content between the opening and closing tag to a static 
* file on your server. The essential difference between this tag and the 
* built-in cfcache tag is that this tag can cache sections of a page,
* rather than the entire page. The key is a unique string that identifies 
* the entry in the cache. You may also use a complex data structure as a 
* key, such as a struct. If you do, then the key will be transformed into 
* a  packet first, which reduces efficiency. You may want to use
* key="#getCurrentTemplatePath()#". directory should be the absolute path
* to an empty folder on your server. Use flush if you want to clear the
* cache. For efficiency, cf_cache does not remove expired items, so you 
* may want to flush the cache occasionally. timespan indicates how long 
* you would like to cache this content. Use createTimeSpan(), e.g. 
* timespan="#createTimeSpan(0, 1, 0, 0)#"
* You may like to use the writeOnly attribute where a site administrator
* is logged in. They can see the changes they make immediately, but the
* cache is still updated. You can set default attributes for directory,
* disabled,flush,timespan, writeOnly in the 
* request.eswsoftware.cache.defaults struct.
* 
* @param key              any simple or complex data structure (required)
* @param directory        cache directory (default=getTempDirectory())
* @param flush            boolean (default=false)
* @param disabled         boolean (default=false)
* @param writeonly        boolean (default=false)
* @param timespan         number representing a time span (default=1 hour)
* @result renders content between tags from cache or generates fresh content
*  as required.
* @author Matthew Walker, WWW.eswsoftware.com
* @version 1.1, 2005-04-24 added disabled, writeonly, and defaults in
*                          request.eswsoftware.cache.defaults. Allow complex
*                          keys.
* @version 1, 2005-04-22
*---><cfsetting enablecfoutputonly="Yes">
	
<!--- initial checks --->
<cfif not thisTag.hasEndTag>
  <cfthrow message="cf_cache requires an end tag." type="Application">
</cfif>
<cfparam name="attributes.key">

<!--- establish defaults --->
<cfparam name="request.eswsoftware"                                         default="#structNew()#">
<cfparam name="request.eswsoftware.cache"                                   default="#structNew()#">
<cfparam name="request.eswsoftware.cache.defaults"                          default="#structNew()#">
<cfparam name="request.eswsoftware.cache.defaults.directory" type="string"  default="#getTempDirectory()#"> 
<cfparam name="request.eswsoftware.cache.defaults.disabled"  type="string"  default="no">
<cfparam name="request.eswsoftware.cache.defaults.flush"     type="boolean" default="no"> 
<cfparam name="request.eswsoftware.cache.defaults.timespan"  type="numeric" default="#createTimeSpan(0,1,0,0)#"> 
<cfparam name="request.eswsoftware.cache.defaults.writeOnly" type="string"  default="no"> 
<cfset request.eswsoftware.cache.version = 1.1>
<cfset structAppend(attributes, request.eswsoftware.cache.defaults, false)>

<!--- main part --->
<cfset output = "">
<cfif not attributes.disabled>
  <cfif thisTag.executionMode eq "start">

    <cfset foundInCache = false>
    <cfif not isSimpleValue(attributes.key)>
		<cfwddx action="CFML2WDDX" input="#attributes.key#" output="attributes.key" usetimezoneinfo="Yes">
	</cfif>
	<cfset filename = "cache_" & hash(attributes.key) & ".HTML">
    
    <cfif attributes.flush>
      <!--- flush cache --->
      <cfdirectory action="LIST" directory="#attributes.directory#" name="cache" filter="cache_*.">
      <cfloop query="cache">
        <cftry>
          <cffile action="DELETE" file="#attributes.directory##name#">
          <cfcatch />
        </cftry>
      </cfloop>
    </cfif>
	
	<cfif not attributes.writeOnly>
      <!--- try to retrieve from cache (check age) --->
      <cftry>
        <cfdirectory action="LIST" directory="#attributes.directory#" name="fileDetails" filter="#filename#">
        <cfset expiryDateTime = now() - attributes.timespan>
	    <cfif fileDetails.recordCount and (fileDetails.dateLastModified gt expiryDateTime)>
          <cffile action="READ" file="#attributes.directory##filename#" variable="output">
          <cfset foundInCache = true>
          <cfoutput>#output#</cfoutput>
		  <cfsetting enablecfoutputonly="No"><cfexit>
        </cfif>
        <cfcatch />
      </cftry>  
    </cfif>
	
  <cfelse>

    <cfif not foundInCache>
      <!--- try to insert into cache --->
      <cftry>
        <cffile action="DELETE" file="#attributes.directory##filename#">
        <cfcatch />
      </cftry>
      <cftry>
        <cffile action="WRITE" file="#attributes.directory##filename#" output="#thisTag.generatedContent#" addnewline="No">
        <cfcatch />
      </cftry>    
    </cfif>
    
  </cfif>  
</cfif>

<cfsetting enablecfoutputonly="No">
World Wide Web
Web Distributed Data eXchange
An XML format for storing structured data.
HyperText Markup Language
The coding language used to create hypertext documents for use on the World Wide Web.
Universally Unique Identifier
a unique 128-bit code.