PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/wheels/cache/functions.cfm

http://cfwheels.googlecode.com/
ColdFusion | 99 lines | 89 code | 10 blank | 0 comment | 5 complexity | 3c4b46aa145019d56daa9f271dfed570 MD5 | raw file
 1<cffunction name="init" returntype="any" access="public" output="false">
 2	<cfargument name="storage" type="string" required="false" default="#application.wheels.cacheStorage#">
 3	<cfargument name="strategy" type="string" required="false" default="#application.wheels.cacheStrategy#">
 4	<cfargument name="defaultCacheTime" type="numeric" required="false" default="#application.wheels.defaultCacheTime#">
 5	<cfargument name="cacheCullPercentage" type="numeric" required="false" default="#application.wheels.cacheCullPercentage#">
 6	<cfargument name="cacheCullInterval" type="numeric" required="false" default="#application.wheels.cacheCullInterval#">
 7	<cfargument name="maximumItemsToCache" type="numeric" required="false" default="#application.wheels.maximumItemsToCache#">
 8	<cfargument name="cacheDatePart" type="string" required="false" default="#application.wheels.cacheDatePart#">
 9	<cfscript>
10		var loc = {};
11		
12		// setup our instance scope
13		variables.$instance = {};
14		StructAppend(variables.$instance, arguments);
15		
16		// the actual cache of items is stored where ever the storage component is supposed to
17		variables.$instance.cache = CreateObject("component", "storage.#arguments.storage#").init(argumentCollection=arguments);
18		variables.$instance.strategy = CreateObject("component", "strategy.#arguments.strategy#").init(argumentCollection=arguments);
19	</cfscript>
20	<cfreturn this>
21</cffunction>
22
23<cffunction name="add" returntype="void" access="public" output="false">
24	<cfargument name="key" type="string" required="true">
25	<cfargument name="value" type="any" required="true">
26	<cfargument name="time" type="numeric" required="false" default="#variables.$instance.defaultCacheTime#">
27	<cfargument name="currentTime" type="date" required="false" default="#Now()#">
28	<cfscript>
29		var loc = {};
30		
31		// evict any
32		if (variables.$instance.strategy.evictOnSet())
33			variables.$instance.cache.evict(strategy=variables.$instance.strategy, currentTime=arguments.currentTime);
34		
35		if (count() < variables.$instance.maximumItemsToCache)
36		{
37			// set our stats in the cache object
38			loc.cacheItem = {};
39			loc.cacheItem.addedAt = arguments.currentTime;
40			loc.cacheItem.expiresAt = DateAdd(variables.$instance.cacheDatePart, arguments.time, arguments.currentTime);
41			loc.cacheItem.hitCount = 0;
42			
43			if (IsSimpleValue(arguments.value))
44				loc.cacheItem.value = arguments.value;
45			else
46				loc.cacheItem.value = Duplicate(arguments.value);
47			
48			// always cache our value along with our metadata
49			variables.$instance.cache.set(arguments.key, loc.cacheItem);
50		}
51	</cfscript>
52</cffunction>
53
54<cffunction name="get" returntype="any" access="public" output="false">
55	<cfargument name="key" type="string" required="true">
56	<cfargument name="currentTime" type="date" required="false" default="#Now()#">
57	<cfscript>
58		var loc = {};
59		
60		// we rely on the storage object to pass back the struct that we passed in, otherwise it is a miss
61		loc.evicted = false;
62		loc.cacheItem = variables.$instance.cache.get(arguments.key);
63		
64		if (IsStruct(loc.cacheItem))
65		{
66			// if we are going to do an eviction here, we should have each storage object handle it with a strategy object
67			// so the strategy object needs to be able to apply it's strategy to a single key / value
68			
69			// do eviction
70			if (variables.$instance.strategy.evictOnGet())
71			{
72				loc.keys = [ arguments.key ];
73				loc.evicted = variables.$instance.cache.evict(keys=loc.keys, strategy=variables.$instance.strategy, currentTime=arguments.currentTime);
74			}
75			
76			if (!loc.evicted)
77			{
78				loc.cacheItem.hitCount++;
79				variables.$instance.cache.set(arguments.key, loc.cacheItem);
80			
81				return loc.cacheItem.value;
82			}
83		}
84	</cfscript>
85	<cfreturn false>
86</cffunction>
87
88<cffunction name="remove" returntype="void" access="public" output="false">
89	<cfargument name="key" type="string" required="true">
90	<cfset variables.$instance.cache.delete(arguments.key)>
91</cffunction>
92
93<cffunction name="count" returntype="numeric" access="public" output="false">
94	<cfreturn variables.$instance.cache.count()>
95</cffunction>
96
97<cffunction name="clear" returntype="void" access="public" output="false">
98	<cfreturn variables.$instance.cache.flush()>
99</cffunction>