Ticket #235: timedCache.cfc

File timedCache.cfc, 3.6 kB (added by RobGonda, 19 years ago)

Suggested fix.

Line 
1<cfcomponent displayname="TimedCache" output="false" hint="I am a timed cache of anything you want to cache.">
2  <cffunction name="Init" access="public" returnType="TimedCache" output="false" hint="I build a new TimedCache.">
3                <cfargument name="defaultTimeout" type="numeric" required="true" />
4
5        <cfset variables.defaultTimeout = arguments.defaultTimeout />
6    <cfset variables.members = structNew() />
7        <cfset variables.gctimestamp = now() /> <!--- last cycle --->
8        <cfset variables.gccycle = 5 /> <!--- minutes between cycles --->
9    <cfreturn this />
10  </cffunction>
11
12  <cffunction name="GetAll" access="public" returnType="struct" output="false" hint="I get all values using StructCopy().">
13    <cfreturn structCopy(variables.members) />
14  </cffunction>
15
16  <cffunction name="SetValue" access="public" returnType="void" output="false" hint="I set a value in the collection.">
17    <cfargument name="name" type="string" required="true" hint="I am the name of the value.">
18    <cfargument name="value" type="any" required="true" hint="I am the value.">
19    <cfargument name="timeout" type="numeric" required="false" hint="I am the [optional] timespan for which this value should be cached." />
20
21    <cfset var item = structNew() />
22    <cfif structKeyExists(arguments, "timeout")>
23            <cfset item.expires = now() * 1 + arguments.timeout />
24          <cfelse>
25            <cfset item.expires = now() * 1 + variables.defaultTimeout />
26                </cfif>
27
28    <cfset item.value = arguments.value />
29
30    <cfset variables.members[arguments.name] = item />
31  </cffunction>
32
33  <cffunction name="GetValue" access="public" returnType="any" output="false" hint="I get a value from the collection.">
34    <cfargument name="name" type="string" required="true" hint="I am the name of the value.">
35
36
37    <cfif Exists(arguments.name)>
38                        <cfreturn variables.members[arguments.name].value />
39                <cfelse>
40                        <cfthrow type="ModelGlue.Util.TimedCache.ItemNotFound" message="TimedCache: Request item not in cache.  Use Exists() to make sure it exists before trying to get it." />
41                </cfif>
42  </cffunction>
43
44  <cffunction name="RemoveValue" access="public" returnType="void" output="false" hint="I remove a value from the collection.">
45    <cfargument name="name" type="string" required="true" hint="I am the name of the value.">
46    <cfset structDelete(variables.members, arguments.name) />
47  </cffunction>
48
49  <cffunction name="Exists" access="public" returnType="boolean" output="false" hint="I state if a value exists.">
50    <cfargument name="name" type="string" required="true" hint="I am the name of the value.">
51
52        <cfset GarbageCollectItem(arguments.name) />
53       
54        <cfif now() gt dateAdd('n', variables.gccycle, variables.gctimestamp)>
55                <cfset GarbageCollect() />
56        </cfif>
57       
58    <cfreturn structKeyExists(variables.members, arguments.name)>
59  </cffunction>
60
61  <cffunction name="GarbageCollectItem" access="private" returnType="void" output="false">
62    <cfargument name="name" type="string" required="true" hint="I am the name of the value.">
63
64        <cfif structKeyExists(variables.members, arguments.name)
65                                        AND now() * 1 gte variables.members[arguments.name].expires>
66                        <cfset structDelete(variables.members, arguments.name) />
67          </cfif>
68        </cffunction>
69
70  <cffunction name="GarbageCollect" access="private" returnType="void" output="false">
71        <cfset var name = '' />
72        <cfloop collection="#variables.members#" item="name">
73                <cfif now() * 1 gte variables.members[name].expires>
74                                <cfset structDelete(variables.members, name) />
75                  </cfif>
76         </cfloop>
77         <cfset variables.gctimestamp = now() />
78        </cffunction>
79
80</cfcomponent>