我一直对从CF8到9的变化视而不见,在这种情况下,如果不创建自定义缓存或其他不值得付出努力的激烈解决方案,就不再可能写入磁盘缓存。
此时,我已经放弃了尝试将应用程序转换为(如果可能的话)将缓存文件的内容写入静态cfm文件的相同方法。我现在更好奇了,因为我已经深入地研究了它。我在找一个比我自己更有经验的人。
我想了解或知道如何使用模板缓存:
这是我所使用的代码,需要Cf9.0.1,因为我相信由于EhCache 2.0的添加,有些缓存内存函数在9.0中不可用。
<cftry>
<cfcache action='serverCache' timeout='#CreateTimeSpan(0,0,0,10)#' stripwhitespace='true'
usequerystring='true'>
Stuff to cache.
<br/>
<cfoutput>#now()#</cfoutput>
<br/>
</cfcache>
<!--- Get the cached contents --->
<cfdump var="#cacheGetProperties()#">
<cfdump var="#getAllTemplateCacheIds()#">
<!---Locate a single cached item --->
<cfscript>
cacheArray = getAllTemplateCacheIds();
WriteOutput("Before<br/>");
for(i=1;i LTE ArrayLen(cacheArray);i=i+1)
{
writeOutput(cacheArray[i] & "<br/>");
if(FindNoCase(scriptPath, cacheArray[i]))
{
//expect only to find min and max one per string so no need to worry about out of bounds
cacheIDSubStr = REFind("[a-fA-F\d]{32}(?=_LINE:\d*$)",cacheArray[i],1,1);
cacheID = Mid(cacheArray[i],CacheIDSubStr.pos[1],CacheIDSubStr.len[1]);
//Failure to delete cache expected fireworks
//WriteOutput(cacheID&"<br/>");
//cacheObject = CacheGet(cacheID);
//CacheRemove(cacheID);
templateCache = cacheGetSession("template");
//Tooling around with the exposed guts of cacheGetSession
WriteDump(templateCache.getKeys());
}
}
</cfscript>
<cfcatch type="Any">
<cfscript>
writeoutput("Error:" & cfcatch.message);
</cfscript>
</cfcatch>
</cftry>不应该存在错误,但是它是从原始编辑到这里发布的。
发布于 2012-03-21 03:24:22
秘密在于CF9.0.1函数cacheGetSession()中,以及对其他函数的修改,以key作为参数。
下面的代码只适用于9.0.1.
假设:
<cfcache action="serverCache" timeout="#CreateTimeSpan(0,0,0,10)#" stripwhitespace="true" usequerystring="true">
Stuff to cache in the default store.
<br/>
<cfoutput>#Now()#</cfoutput>
</cfcache>
<cfcache action="serverCache" key="myCustomCache" timeout="#CreateTimeSpan(0,0,0,10)#" stripwhitespace="true" usequerystring="true">
Stuff to cache in the 'myCustomCache' store.
<cfoutput>#Now()#</cfoutput>
</cfcache>分别访问缓存的标识符
默认(模板):
<cfdump var=#getAllTemplateCacheIds()#>myCustomCache:
<cfdump var=#cacheGetSession('myCustomCache',true).getKeys()#>读取自定义缓存的数据
<cfset keys = cacheGetSession('myCustomCache',true).getKeys() />
<cfcache key="myCustomCache" action="get" name="dataInCache" id="#keys[1]#">
<cfdump var=#dataInCache#>独立于默认刷新自定义缓存
<cfset cacheRemove(keys[1],true,'myCustomCache')>遗憾的是,文档在9.0.1中潜入的功能方面还不是最好的。幸运的是,CF用户基础擅长挖掘这些东西,即罗布-布鲁克斯·比尔森,他有很多关于CF和缓存的优秀文章。
在以这种方式使用缓存时,您应该记住几个警告:
IsNull()中以进行验证。所以,要小心冲洗适当的数据..。
https://stackoverflow.com/questions/9745847
复制相似问题