PageRenderTime 64ms CodeModel.GetById 59ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/wheels/cache/storage/Memcached.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 97 lines | 82 code | 15 blank | 0 comment | 5 complexity | edf31c88e04f1d43c4c0a529ec78a319 MD5 | raw file
 1<cfcomponent implements="AbstractStorage" output="false">
 2	
 3	<cffunction name="init" access="public" output="false" returntype="any">
 4		<cfargument name="servers" type="string" required="false" default="127.0.0.1:11211" />
 5		<cfargument name="timeout" type="numeric" required="false" default="60" />
 6		<cfargument name="timeunit" type="string" required="false" default="SECONDS" />
 7		<cfscript>
 8			variables.$instance = {};
 9			variables.$instance.cache = false;
10			variables.$instance.available = false;
11			StructAppend(variables.$instance, arguments);
12			variables.$instance.cache = variables.$connect();
13		</cfscript>
14		<cfreturn this>
15	</cffunction>
16	
17	<cffunction name="isAvailable" access="public" output="false" returntype="boolean">
18		<cfreturn IsObject(variables.$instance.cache)>
19	</cffunction>
20	
21	<cffunction name="set" access="public" output="false" returntype="void">
22		<cfargument name="key" type="string" required="true">
23		<cfargument name="value" type="any" required="true">
24		<cfscript>
25			variables.$instance.cache.set(arguments.key, arguments.value);
26		</cfscript>
27	</cffunction>
28	
29	<cffunction name="get" access="public" output="false" returntype="any">
30		<cfargument name="key" type="string" required="true">
31		<cfscript>
32			var loc = {};
33			
34			loc.value = variables.$instance.cache.get(arguments.key);
35			
36			if (!StructKeyExists(loc, "value") || (IsSimpleValue(loc.value) && !Len(Trim(loc.value))))
37				loc.value = false;
38		</cfscript>
39		<cfreturn loc.value>
40	</cffunction>
41	
42	<cffunction name="evict" access="public" output="false" returntype="numeric">
43		<cfargument name="keys" type="array" required="false" default="#ArrayNew(1)#">
44		<cfargument name="strategy" type="any" required="true">
45		<cfargument name="currentTime" type="date" required="true">
46		<!--- don't do anything as memcached decides when to evict content from the cache with it's internal lru logic --->
47		<cfreturn 0>
48	</cffunction>
49	
50	<cffunction name="delete" access="public" output="false" returntype="void">
51		<cfargument name="key" type="string" required="true">
52		<cfscript>
53			variables.$instance.cache.delete(arguments.key);
54		</cfscript>
55	</cffunction>
56	
57	<cffunction name="count" access="public" output="false" returntype="numeric">
58		<cfscript>
59			var loc = {};
60			loc.stats = variables.$instance.cache.getStats();
61			loc.totalItems = 0;
62			
63			for (loc.item in loc.stats)
64				loc.totalItems = loc.totalItems + (loc.stats[loc.item]["curr_items"] - 2); // for some reason memcached always shows two items in the cache over what there really is
65		</cfscript>
66		<cfreturn loc.totalItems>
67	</cffunction>
68	
69	<cffunction name="flush" access="public" output="false" returntype="void">
70		<cfscript>
71			variables.$instance.cache.flush();
72		</cfscript>
73	</cffunction>
74	
75	<cffunction name="$connect" access="private" output="false" returntype="any">
76		<cfargument name="servers" type="string" required="false" default="#Trim(REReplace(variables.$instance.servers, '\s+', ' ', 'all'))#">
77		<cfargument name="defaultTimeout" type="string" required="false" default="#variables.$instance.timeout#">
78		<cfargument name="defaultUnit" type="string" required="false" default="#variables.$instance.timeunit#">
79		<cfscript>
80			var loc = {};
81			
82			try
83			{
84				loc.factory = CreateObject("component", "wheelsMapping.vendor.memcached.MemcachedFactory").init(argumentCollection=arguments);
85				loc.memcached = loc.factory.getMemcached();
86				
87				if (!StructIsEmpty(loc.memcached.getVersions()))
88					return loc.memcached;
89			}
90			catch (Any e)
91			{
92			}
93		</cfscript>
94		<cfreturn false>
95	</cffunction>	
96	
97</cfcomponent>