<cfcomponent
output="false"
hint="Handles simple time-based data caching.">
<cfset VARIABLES.Instance = StructNew() />
<cfset VARIABLES.Instance.Data = StructNew() />
<cffunction
name="Init"
access="public"
returntype="any"
output="false"
hint="Returns an initialized component.">
<cfreturn THIS />
</cffunction>
<cffunction
name="GetData"
access="public"
returntype="any"
output="false"
hint="Returns the given data item from the data cache (will throw exception if the requested data does not exist).">
<cfargument
name="Key"
type="string"
required="true"
hint="The unique key for this data entry."
/>
<cfset VARIABLES.UpdateCacheData(
ARGUMENTS.Key
) />
<cfreturn VARIABLES.Instance.Data[ ARGUMENTS.Key ].Data />
</cffunction>
<cffunction
name="HasData"
access="public"
returntype="boolean"
output="false"
hint="Checks to see if the given data item exists in the cache.">
<cfargument
name="Key"
type="string"
required="true"
hint="The unique key for this data entry."
/>
<cfset VARIABLES.UpdateCacheData(
ARGUMENTS.Key
) />
<cfreturn StructKeyExists(
VARIABLES.Instance.Data,
ARGUMENTS.Key
) />
</cffunction>
<cffunction
name="SetData"
access="public"
returntype="void"
output="false"
hint="Sets the data in the cache.">
<cfargument
name="Key"
type="string"
required="true"
hint="The unique key for this data entry."
/>
<cfargument
name="Data"
type="any"
required="true"
hint="The data being stored at the given key."
/>
<cfargument
name="ExpirationDate"
type="numeric"
required="true"
hint="The date on which this data will expire and be removed from the cache."
/>
<cfset var LOCAL = StructNew() />
<cfset LOCAL.Item = StructNew() />
<cfset LOCAL.Item.Data = ARGUMENTS.Data />
<cfset LOCAL.Item.ExpirationDate = ARGUMENTS.ExpirationDate />
<cfset VARIABLES.Instance.Data[ ARGUMENTS.Key ] = LOCAL.Item />
<cfreturn />
</cffunction>
<cffunction
name="UpdateCacheData"
access="private"
returntype="void"
output="false"
hint="Checks to see if the given data tiem needs to be removed from the cache (and removes it if necessary).">
<cfargument
name="Key"
type="string"
required="true"
hint="The unique key for this data entry."
/>
<cfif (VARIABLES.Instance.Data[ ARGUMENTS.Key ].ExpirationDate LTE Now())>
<cfset StructDelete(
VARIABLES.Instance.Data,
ARGUMENTS.Key
) />
</cfif>
<cfreturn />
</cffunction>
</cfcomponent>