这真让我抓狂。我们尝试构建一个 CF memcached 包装器。我们有一个这样的 memcached.cfc 组件:
<cfset this.m = arraynew(1)>
<cffunction name="init" access="public" output="false">
<cfif not isdefined("application.memcached")
....
<cfscript>
setup();
</cfscript>
...
<cfset application.memcached = this>
</cfif>
<cfreturn application.memcached>
</cffunction>
<cffunction name="setup" access="private" output="false">
<cftry>
<cfset this.m = arraynew(1)>
<cfloop from="1" to="#this.poolSize#" index="i">
<cfset this.m[i] = createClient()>
</cfloop>
<cflog application="no" file="memcached" text="Successfully set up #this.poolSize# new memcache clients">
<cfcatch>
<cflog application="no" file="memcached" text="Exception in setup() while setting up the pool: type: #cfcatch.type#, message: #cfcatch.message#, detail: #cfcatch.detail#">
</cfcatch>
</cftry>
</cffunction>
<cffunction name="createClient" access="private" output="false">
<cfset var AU = createObject("java", "net.spy.memcached.AddrUtil").init()>
<cfset var c = createObject("java", "net.spy.memcached.MemcachedClient").init(AU.getAddresses("127.0.0.1:11211"))>
<cfreturn c>
</cffunction>
<cffunction name="getCache" access="public" returntype="any" output="false">
<cfset idx = ceiling(rand() * 20)>
<cfreturn application.memcached.m[idx]>
</cffunction>
奇怪的是,运行 30 分钟左右后,getCache 开始失败,表明 application.memcached.m 数组中的位置 idx 处没有项目。
这怎么可能发生?CF 数组是使用弱引用还是什么?当然,一旦阵列填充了 20 个客户端,阵列应该始终保持满吗?
每个新客户端都会产生一个新线程,因此一旦我们失去对客户端的引用,现在就有办法将其关闭,并且该线程永远存在于那里并占用内存。请问,我错过了什么?