Skip to content

Instantly share code, notes, and snippets.

@ryanguill
Created September 14, 2012 01:45
Show Gist options
  • Save ryanguill/3719320 to your computer and use it in GitHub Desktop.
Save ryanguill/3719320 to your computer and use it in GitHub Desktop.
cache/facade example
<cfcomponent>
<cfset variables.settings = structNew() />
<cfset variables.settings.cacheExpirationSeconds = 10 />
<cfset variables.instance = structNew() />
<cfset variables.instance.cacheExpirationTime = 0 />
<cfset variables.instance.cache = queryNew("foo") />
<cffunction name="init" access="public" returntype="any" output="false" hint="">
<cfargument name="cacheExpirationSeconds" type="numeric" required="True" />
<cfset variables.settings.cacheExpirationSettings = 10 />
</cffunction>
<cffunction name="isCacheExpired" access="private" returntype="boolean" output="false" hint="">
<cfreturn getTickCount() GTE variables.instance.cacheExpirationTime />
</cffunction>
<cffunction name="resetCacheExpiration" access="private" returntype="void" output="false" hint="">
<cfset variables.instance.cacheExpirationTime = getTickCount() + (variables.cacheExpirationSeconds * 1000) />
</cffunction>
<cffunction name="getCacheSize" access="private" returntype="numeric" output="false" hint="">
<cfreturn variables.instance.cache.recordCount />
</cffunction>
<cffunction name="updateCache" access="public" returntype="boolean" output="false" hint="">
<cfif isCacheExpired() OR NOT getCacheSize()>
<cfinvoke webservice="fooWebservice.cfc?wsdl" method="getAllTheData" returnvariable="variables.instance.cache">
<cfinvokeargument name="whateverArguments" value="foobar" />
</cfinvoke>
<cfset resetCacheExpiration() />
</cfif>
</cffunction>
<cffunction name="searchData" access="public" returntype="query" output="false" hint="">
<cfargument name="q" type="string" required="True" />
<cfset updateCache() />
<cfquery name="local.results" dbtype="query">
SELECT * FROM variables.instance.cache
WHERE
foo = '%#arguments.q#%'
</cfquery>
<cfreturn local.results />
</cffunction>
</cfcomponent>
<!--- your ajax call calls this facade method --->
<cffunction name="searchData" access="remote" returntype="query" output="false" hint="">
<cfargument name="q" type="string" required="True" />
<cfif NOT structKeyExists(application,"foo")>
<cfset application.foo = createObject("component","foo").init(600) />
</cfif>
<cfreturn application.foo.searchData(arguments.q) />
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment