CacheManager

Package: MachII.framework
Inherits from: framework.CommandLoaderBase
Provides an unified API for event and subroutine caching in Mach-II.

<!--- License: Copyright 2008 GreatBizTools, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Copyright: GreatBizTools, LLC Author: Kurt Wiersma (kurt@mach-ii.com) $Id: CacheManager.cfc 595 2007-12-17 02:39:01Z kurtwiersma $ Created version: 1.6.0 Updated version: 1.6.0 Notes: --->

Method Summary
public CacheManager init(AppManager appManager, [any parentCacheManager=""])

Initializes the manager.

public void addCacheHandler(CacheHandler cacheHandler)

Adds a cache handler.

public void clearCacheByName(string cacheName, Event event)

Clears caches by cacheName.

public void clearCachesByAlias(string alias, Event event, [string criteria=""])

Clears caches by alias.

public void configure()

Configures the cache handlers.

public void disableCaching()

Disables caching.

public void enableCaching()

Enables caching.

public AppManager getAppManager()
public CacheHandler getCacheHandler(string handlerId)

Gets a cache handler by handlerId.

public struct getCacheHandlers()

Gets all cache handlers in a struct keyed by the handlerId.

public struct getCacheHandlersByAlias(string alias)

Gets cache handlers by alias.

public struct getCacheHandlersByEventName(string eventName)

Gets all cache handlers by event name.

public struct getCacheHandlersBySubroutine(string subroutineName)

Gets all cache handlers in subroutine name.

public any getCacheStrategyManager()

Sets the CacheStrategyManager. Returns empty string if no manager is defind.

public string getDefaultCacheName()
private string getKeyHash(string keyName)

Gets a key name hash (uppercase and hash the key name)

private Log getLog()

Gets the log.

public any getParent()

Sets the parent CacheManager instance this CacheManager belongs to. It will return empty string if no parent is defined.

public boolean isAliasDefined(string alias)

Checks if an alias is current defined and in use.

public boolean isCacheHandlerDefined(string handlerId)

Checks if a cache handler is defined.

public string loadCacheHandlerFromXml(string configXML, string parentHandlerName, string parentHandlerType)

Loads a cache handler from Xml.

public void removeCacheHandler(CacheHandler cacheHandler)

Removes a cache handler.

public void setAppManager(AppManager appManager)
public void setCacheStrategyManager(CacheStrategyManager cacheStrategyManager)

Returns the CacheStrategyManager.

public string setDefaultCacheName(string defaultCacheName)
private void setLog(LogFactory logFactory)

Uses the log factory to create a log.

public void setParent(CacheManager parentCacheManager)

Returns the parent CacheManager instance this CacheManager belongs to.

Methods inherited from framework.CommandLoaderBase:   setupCache , setupPublish , setupRedirect , setupFilter , setupExecute , setupEventArg , setupAnnounce , createCommand , setupViewPage , setupDefault , setupCacheClear , setupNotify , setupEventMapping , setupEventBean
Method Detail
addCacheHandler

public void addCacheHandler( CacheHandler cacheHandler )

Adds a cache handler.

Parameters:
CacheHandler cacheHandler

Code:

	<cffunction name="addCacheHandler" access="public" returntype="void" output="false"
		hint="Adds a cache handler.">
		<cfargument name="cacheHandler" type="MachII.framework.CacheHandler" required="true"
			hint="The cache handler you want to add." />

		<cfset var handlerId = arguments.cacheHandler.getHandlerId() />
		<cfset var alias = arguments.cacheHandler.getAlias() />
		<cfset var cacheName = arguments.cacheHandler.getCacheName() />
		<cfset var handlerType = arguments.cacheHandler.getParentHandlerType() />

		
		<cfset StructInsert(variables.handlers, handlerId, arguments.cacheHandler, false) />
		
		
		<cfif handlerType EQ "event">
			<cfset variables.handlersByEventName[handlerId][getKeyHash(arguments.cacheHandler.getParentHandlerName())] = true />
		<cfelseif handlerType EQ "subroutine">
			<cfset variables.handlersBySubroutineName[handlerId][getKeyHash(arguments.cacheHandler.getParentHandlerName())] = true />
		</cfif>
		
		<cfset variables.handlersByName[getKeyHash(cacheName)] = handlerId />
		
		
		<cfif Len(alias)>
			<cfset variables.handlersByAliases[getKeyHash(alias)][handlerId] = true />
		</cfif>
	</cffunction> 

clearCacheByName

public void clearCacheByName( string cacheName, Event event )

Clears caches by cacheName.

Parameters:
string cacheName
Event event

Code:

	<cffunction name="clearCacheByName" access="public" returntype="void" output="false"
		hint="Clears caches by cacheName.">
		<cfargument name="cacheName" type="string" required="true" />
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var handlerId = "" />
		<cfset var keyHashed = getKeyHash(arguments.cacheName) />
		
		<cfif log.isDebugEnabled()>
			<cfset log.debug("CacheManager clear cache for '#arguments.cacheName#' (#keyHashed#), " &
					"exists: #StructKeyExists(variables.handlersByName, keyHashed)#, " &
					"handler keys: #StructKeyList(variables.handlersByName)#.") />
		</cfif>
		
		
		<cfif StructKeyExists(variables.handlersByName, keyHashed)>
			<cfset handlerId = variables.handlersByName[keyHashed] />
			<cfset getCacheHandler(handlerId).clearCache(arguments.event) />
		</cfif>
	</cffunction> 

clearCachesByAlias

public void clearCachesByAlias( string alias, Event event, [string criteria=""] )

Clears caches by alias.

Parameters:
string alias
Event event
[string criteria=""]

Code:

	<cffunction name="clearCachesByAlias" access="public" returntype="void" output="false"
		hint="Clears caches by alias.">
		<cfargument name="alias" type="string" required="true" />
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		<cfargument name="criteria" type="string" required="false" default="" />
		
		<cfset var cacheHandlers = StructNew() />
		<cfset var key = "" />

		
		<cfif StructKeyExists(variables.handlersByAliases, getKeyHash(arguments.alias))>
			<cfset cacheHandlers = variables.handlersByAliases[getKeyHash(arguments.alias)] />
			
			<cfloop collection="#cacheHandlers#" item="key">
				<cfset getCacheHandler(key).clearCache(event, criteria) />
			</cfloop>
		</cfif>
	</cffunction> 

configure

public void configure( )

Configures the cache handlers.

Parameters:

Code:

	<cffunction name="configure" access="public" returntype="void" output="false"
		hint="Configures the cache handlers.">
		
		<cfset var handlerId = "" />
		<cfset var cacheStrategy = "" />
		<cfset var cacheName = "" />
		<cfset var cacheStrategyManager = getCacheStrategyManager() />
		
		
		<cfset cacheStrategyManager.configure() />
		
		
		<cfif IsObject(getParent()) AND Len(getParent().getDefaultCacheName()) 
			AND NOT Len(getDefaultCacheName())>
			<cfset setDefaultCacheName(getParent().getDefaultCacheName()) />
		</cfif>
		
		
		<cfloop collection="#variables.handlers#" item="handlerId">
			<cfset cacheName = variables.handlers[handlerId].getCacheName() />
			
			
			<cfif NOT Len(cacheName)>
				<cfset cacheName = getDefaultCacheName() />
			</cfif>
			
			
			<cfset cacheStrategy = cacheStrategyManager.getCacheStrategyByName(cacheName) />
			<cfset variables.handlers[handlerId].setCacheStrategy(cacheStrategy) />
		</cfloop>
	</cffunction> 

disableCaching

public void disableCaching( )

Disables caching.

Parameters:

Code:

	<cffunction name="disableCaching" access="public" returntype="void" output="false"
		hint="Disables caching.">
		
		<cfset var key = "" />
		<cfset var handlers = getCacheHandlers() />
		
		<cfloop collection="#handlers#" item="key">
			<cfset handlers[key].disableCaching() />
		</cfloop>
	</cffunction> 

enableCaching

public void enableCaching( )

Enables caching.

Parameters:

Code:

	<cffunction name="enableCaching" access="public" returntype="void" output="false"
		hint="Enables caching.">
			
		<cfset var key = "" />
		<cfset var handlers = getCacheHandlers() />
		
		<cfloop collection="#handlers#" item="key">
			<cfset handlers[key].enableCaching() />
		</cfloop>
	</cffunction> 

getAppManager

public AppManager getAppManager( )

Parameters:

Code:

	<cffunction name="getAppManager" access="public" returntype="MachII.framework.AppManager" output="false">
		<cfreturn variables.appManager />
	</cffunction> 

getCacheHandler

public CacheHandler getCacheHandler( string handlerId )

Gets a cache handler by handlerId.

Parameters:
string handlerId

Code:

	<cffunction name="getCacheHandler" access="public" returntype="MachII.framework.CacheHandler" output="false"
		hint="Gets a cache handler by handlerId.">
		<cfargument name="handlerId" type="string" required="true"
			hint="Handler id of the cache handler you want to get." />
		<cfreturn variables.handlers[arguments.handlerId] />
	</cffunction> 

getCacheHandlers

public struct getCacheHandlers( )

Gets all cache handlers in a struct keyed by the handlerId.

Parameters:

Code:

	<cffunction name="getCacheHandlers" access="public" returntype="struct" output="false"
		hint="Gets all cache handlers in a struct keyed by the handlerId.">
		<cfreturn variables.handlers />
	</cffunction> 

getCacheHandlersByAlias

public struct getCacheHandlersByAlias( string alias )

Gets cache handlers by alias.

Parameters:
string alias

Code:

	<cffunction name="getCacheHandlersByAlias" access="public" returntype="struct" output="false"
		hint="Gets cache handlers by alias.">
		<cfargument name="alias" type="string" required="true" />
		
		<cfset var cacheHandlers = StructNew() />
		<cfset var handlerIds = StructNew() />
		<cfset var key = "" />
		
		<cfif StructKeyExists(variables.handlersByAliases, getKeyHash(arguments.alias))>
			<cfset handlerIds = variables.handlersByAliases[getKeyHash(arguments.alias)] />
			
			<cfloop collection="#handlerIds#" item="key">
				<cfset cacheHandlers[key] = getCacheHandler(key) />
			</cfloop>
		</cfif>
		
		<cfreturn cacheHandlers />
	</cffunction> 

getCacheHandlersByEventName

public struct getCacheHandlersByEventName( string eventName )

Gets all cache handlers by event name.

Parameters:
string eventName

Code:

	<cffunction name="getCacheHandlersByEventName" access="public" returntype="struct" output="false"
		hint="Gets all cache handlers by event name.">
		<cfargument name="eventName" type="string" required="true" />
		
		<cfset var cacheHandlers = StructNew() />
		<cfset var handlerIds = StructNew() />
		<cfset var key = "" />
		
		<cfif StructKeyExists(variables.handlersByEventName, getKeyHash(arguments.eventName))>
			<cfset handlerIds = variables.handlersByEventName[getKeyHash(arguments.eventName)] />
			
			<cfloop collection="#handlerIds#" item="key">
				<cfset cacheHandlers[key] = getCacheHandler(key) />
			</cfloop>
		</cfif>
		
		<cfreturn cacheHandlers />
	</cffunction> 

getCacheHandlersBySubroutine

public struct getCacheHandlersBySubroutine( string subroutineName )

Gets all cache handlers in subroutine name.

Parameters:
string subroutineName

Code:

	<cffunction name="getCacheHandlersBySubroutine" access="public" returntype="struct" output="false"
		hint="Gets all cache handlers in subroutine name.">
		<cfargument name="subroutineName" type="string" required="true" />
		
		<cfset var cacheHandlers = StructNew() />
		<cfset var handlerIds = StructNew() />
		<cfset var key = "" />
		
		<cfif StructKeyExists(variables.handlersBySubroutineName, getKeyHash(arguments.subroutineName))>
			<cfset handlerIds = variables.handlersBySubroutineName[getKeyHash(arguments.subroutineName)] />
			
			<cfloop collection="#handlerIds#" item="key">
				<cfset cacheHandlers[key] = getCacheHandler(key) />
			</cfloop>
		</cfif>
		
		<cfreturn cacheHandlers />
	</cffunction> 

getCacheStrategyManager

public any getCacheStrategyManager( )

Sets the CacheStrategyManager. Returns empty string if no manager is defind.

Parameters:

Code:

	<cffunction name="getCacheStrategyManager" access="public" returntype="any" output="false"
		hint="Sets the CacheStrategyManager. Returns empty string if no manager is defind.">
		<cfreturn variables.cacheStrategyManager />
	</cffunction> 

getDefaultCacheName

public string getDefaultCacheName( )

Parameters:

Code:

	<cffunction name="getDefaultCacheName" access="public" returntype="string" output="false">
		<cfreturn variables.defaultCacheName />
	</cffunction> 

getKeyHash

private string getKeyHash( string keyName )

Gets a key name hash (uppercase and hash the key name)

Parameters:
string keyName

Code:

	<cffunction name="getKeyHash" access="private" returntype="string" output="false"
		hint="Gets a key name hash (uppercase and hash the key name)">
		<cfargument name="keyName" type="string" required="true" />
		<cfreturn Hash(UCase(arguments.keyName)) />
	</cffunction> 

getLog

private Log getLog( )

Gets the log.

Parameters:

Code:

	<cffunction name="getLog" access="private" returntype="MachII.logging.Log" output="false"
		hint="Gets the log.">
		<cfreturn variables.log />
	</cffunction> 

getParent

public any getParent( )

Sets the parent CacheManager instance this CacheManager belongs to. It will return empty string if no parent is defined.

Parameters:

Code:

	<cffunction name="getParent" access="public" returntype="any" output="false"
		hint="Sets the parent CacheManager instance this CacheManager belongs to. It will return empty string if no parent is defined.">
		<cfreturn variables.parentCacheManager />
	</cffunction> 

init

public CacheManager init( AppManager appManager, [any parentCacheManager=""] )

Initializes the manager.

Parameters:
AppManager appManager
[any parentCacheManager=""]

Code:

	<cffunction name="init" access="public" returntype="CacheManager" output="false"
		hint="Initializes the manager.">
		<cfargument name="appManager" type="MachII.framework.AppManager" required="true" />
		<cfargument name="parentCacheManager" type="any" required="false" default=""
			hint="Optional argument for a parent cache manager. If not defined, default to zero-length string." />
		
		<cfset setAppManager(arguments.appManager) />
		
		<cfset setCacheStrategyManager(CreateObject("component", "MachII.caching.CacheStrategyManager").init()) />
		
		<cfif IsObject(arguments.parentCacheManager)>
			<cfset setParent(arguments.parentCacheManager) />
			<cfset getCacheStrategyManager().setParent(getParent().getCacheStrategyManager()) />
		</cfif>
		
		
		<cfset setLog(getAppManager().getLogFactory()) />
		
		<cfreturn this />
	</cffunction> 

isAliasDefined

public boolean isAliasDefined( string alias )

Checks if an alias is current defined and in use.

Parameters:
string alias

Code:

	<cffunction name="isAliasDefined" access="public" returntype="boolean" output="false"
		hint="Checks if an alias is current defined and in use.">
		<cfargument name="alias" type="string" required="true" />
		
		<cfif StructKeyExists(variables.aliases, arguments.alias)>
			<cfreturn true />
		<cfelse>
			<cfreturn false />
		</cfif>
	</cffunction> 

isCacheHandlerDefined

public boolean isCacheHandlerDefined( string handlerId )

Checks if a cache handler is defined.

Parameters:
string handlerId

Code:

	<cffunction name="isCacheHandlerDefined" access="public" returntype="boolean" output="false"
		hint="Checks if a cache handler is defined.">
		<cfargument name="handlerId" type="string" required="true" 
			hint="Handler id of the cache handler you want to check." />
		<cfreturn StructKeyExists(variables.handlers, arguments.handlerId) />
	</cffunction> 

loadCacheHandlerFromXml

public string loadCacheHandlerFromXml( string configXML, string parentHandlerName, string parentHandlerType )

Loads a cache handler from Xml.

Parameters:
string configXML
string parentHandlerName
string parentHandlerType

Code:

	<cffunction name="loadCacheHandlerFromXml" access="public" returntype="string" output="false"
		hint="Loads a cache handler from Xml.">
		<cfargument name="configXML" type="string" required="true" />
		<cfargument name="parentHandlerName" type="string" required="true" />
		<cfargument name="parentHandlerType" type="string" required="true" />
		
		<cfset var nestedCommandNodes = arguments.configXML.xmlChildren />
		<cfset var command = "" />

		<cfset var cacheStrategy = "" />
		<cfset var cacheHandler = "" />		

		<cfset var alias = "" />
		<cfset var criteria = "" />
		<cfset var cacheName = "" />
		<cfset var i = 0 />
		
		<cfif StructKeyExists(arguments.configXML.xmlAttributes, "alias")>
			<cfset alias = arguments.configXML.xmlAttributes["alias"] />
		</cfif>
		<cfif StructKeyExists(arguments.configXML.xmlAttributes, "criteria")>
			<cfset criteria = arguments.configXML.xmlAttributes["criteria"] />
		</cfif>
		<cfif StructKeyExists(arguments.configXML.xmlAttributes, "name")>
			<cfset cacheName = arguments.configXML.xmlAttributes["name"] />
		</cfif>
		
		
		<cfset cacheHandler = CreateObject("component", "MachII.framework.CacheHandler").init(
			alias, cacheName, criteria, arguments.parentHandlerName, arguments.parentHandlerType) />
		<cfset cacheHandler.setLog(getAppManager().getLogFactory()) />
		<cfloop from="1" to="#ArrayLen(nestedCommandNodes)#" index="i">
			<cfset command = createCommand(nestedCommandNodes[i]) />
			<cfset cacheHandler.addCommand(command) />
		</cfloop>
		
		
		<cfset addCacheHandler(cacheHandler) />
		
		<cfreturn cacheHandler.getHandlerId() />
	</cffunction> 

removeCacheHandler

public void removeCacheHandler( CacheHandler cacheHandler )

Removes a cache handler.

Parameters:
CacheHandler cacheHandler

Code:

	<cffunction name="removeCacheHandler" access="public" returntype="void" output="false"
		hint="Removes a cache handler.">
		<cfargument name="cacheHandler" type="MachII.framework.CacheHandler" required="true"
			hint="The cache handler you want to remove." />

		<cfset var handlerId = arguments.cacheHandler.getHandlerId() />
		<cfset var alias = arguments.cacheHandler.getAlias() />
		<cfset var handlerType = arguments.cacheHandler.getParentHandlerType() />

		
		<cfset StructDelete(variables.handlers, handlerId, false) />
		
		
		<cfif handlerType EQ "event">
			<cfset StructDelete(variables.handlersByEventName[handlerId], getKeyHash(arguments.cacheHandler.getParentHandlerName()), true) />
		<cfelseif handlerType EQ "subroutine">
			<cfset StructDelete(variables.handlersBySubroutineName[handlerId], getKeyHash(arguments.cacheHandler.getParentHandlerName()), true) />
		</cfif>
		
		
		<cfset StructDelete(variables.handlersByName, getKeyHash(cacheName)) />
		
		
		<cfif Len(alias)>
			<cfset StructDelete(variables.handlersByAliases[getKeyHash(alias)], handlerId, false) />
		</cfif>
	</cffunction> 

setAppManager

public void setAppManager( AppManager appManager )

Parameters:
AppManager appManager

Code:

	<cffunction name="setAppManager" access="public" returntype="void" output="false">
		<cfargument name="appManager" type="MachII.framework.AppManager" required="true" />
		<cfset variables.appManager = arguments.appManager />
	</cffunction> 

setCacheStrategyManager

public void setCacheStrategyManager( CacheStrategyManager cacheStrategyManager )

Returns the CacheStrategyManager.

Parameters:
CacheStrategyManager cacheStrategyManager

Code:

	<cffunction name="setCacheStrategyManager" access="public" returntype="void" output="false"
		hint="Returns the CacheStrategyManager.">
		<cfargument name="cacheStrategyManager" type="MachII.caching.CacheStrategyManager" required="true" />
		<cfset variables.cacheStrategyManager = arguments.cacheStrategyManager />
	</cffunction> 

setDefaultCacheName

public string setDefaultCacheName( string defaultCacheName )

Parameters:
string defaultCacheName

Code:

	<cffunction name="setDefaultCacheName" access="public" returntype="string" output="false">
		<cfargument name="defaultCacheName" type="string" required="true" />
		<cfif getCacheStrategyManager().isCacheStrategyDefined(arguments.defaultCacheName, true)>
			<cfset variables.defaultCacheName = arguments.defaultCacheName />
		<cfelse>
			<cfthrow type="MachII.framework.DefaultCacheNameNotAvailable"
				message="The 'defaultCacheName' was set to '#arguments.defaultCacheName#'. This strategy that is not available. Please set the default to a strategy that is configured."
				detail="Available strategies:#ArrayToList(getCacheStrategyManager().getCacheStrategyNames())#" />
		</cfif>
	</cffunction> 

setLog

private void setLog( LogFactory logFactory )

Uses the log factory to create a log.

Parameters:
LogFactory logFactory

Code:

	<cffunction name="setLog" access="private" returntype="void" output="false"
		hint="Uses the log factory to create a log.">
		<cfargument name="logFactory" type="MachII.logging.LogFactory" required="true" />
		<cfset variables.log = arguments.logFactory.getLog(getMetadata(this).name) />
	</cffunction> 

setParent

public void setParent( CacheManager parentCacheManager )

Returns the parent CacheManager instance this CacheManager belongs to.

Parameters:
CacheManager parentCacheManager

Code:

	<cffunction name="setParent" access="public" returntype="void" output="false"
		hint="Returns the parent CacheManager instance this CacheManager belongs to.">
		<cfargument name="parentCacheManager" type="MachII.framework.CacheManager" required="true" />
		<cfset variables.parentCacheManager = arguments.parentCacheManager />
	</cffunction>