
實(shí)現(xiàn)代碼如下:

Dimrs,存技arr

rs.Openconn,sql,1,1
arr=rs.GetRows()
Application.Lock()
Application("cache")=arr
Applicatoin.UnLock()
在VBScript里,數(shù)組是術(shù)的實(shí)際可以存到Application對(duì)象里的,但是代碼如果ASP的語(yǔ)言選擇為JScript的話,那么就有些不妙了,中使我們?cè)谑褂肁pplication儲(chǔ)存一個(gè)數(shù)組時(shí),用緩會(huì)出現(xiàn)以下錯(cuò)誤:
引用內(nèi)容:
Applicationobject,存技ASP0197(0x80004005)
Disallowedobjectuse
Cannotaddobjectwithapartmentmodelbehaviortotheapplicationintrinsicobject.
在微軟的知識(shí)庫(kù)可以找到具體原因如下:
引用內(nèi)容:
JScriptarraysareconsideredtobe"Apartment"COMcomponents.OnlyComponentObjectModel(COM)componentsthataggregatetheFreeThreadedMarshaler(FTM)canbeassignedtoApplicationscopewithinanInternetInformationServer(IIS)5.0ASPpage.Becausean"Apartment"componentcannotaggregatetheFTM(itcannotallowadirectpointertobepassedtoitsclients,unlikea"BothwithFTM"object),JScriptarraysdonotaggregatetheFTM.Therefore,JScriptarrayscannotbeassignedtoApplicationscopefromanASPpage.
以上描述引用自:PRB:ErrorWhenYouStoreaJScriptArrayinApplicationScopeinIIS5.0
因此,為了解決這個(gè)問(wèn)題,術(shù)的實(shí)際在Google里找了一大會(huì),代碼終于找到了一篇文章《Application對(duì)象的中使Contents和StaticObjects做Cache的一些結(jié)論》,解決了這個(gè)問(wèn)題,用緩方法就是存技使用Application.StaticObject存放一個(gè)Scripting.Dictionary對(duì)象,然后再使用Scripting.Dictionary對(duì)象來(lái)存放需要緩存的術(shù)的實(shí)際數(shù)據(jù)。
據(jù)此,代碼寫了一個(gè)操作緩存的類,實(shí)現(xiàn)put、get、remove和clear方法,使用之前,需要在global.asa中添加一個(gè)object:
程序代碼:
<objectid="xbsCache"runat="server"scope="Application"progid="Scripting.Dictionary"></object>
類的實(shí)現(xiàn)如下:
實(shí)現(xiàn)代碼如下:
<scriptlanguage="JScript"runat="server">
/**
Title:cacheoperateclass
Description:operatesystemcache
@Copyright:Copyright(c)2007
@Author:xujiwei
@Website:http://www.xujiwei.cn/
@Version:1.0
@Time:2007-06-2912:03:45
**/
varxbsCache={
get:function(key){
returnApplication.StaticObjects("xbsCache").Item("Cache."+key);
},
put:function(key,data){
Application.Lock();
Application.StaticObjects("xbsCache").Item("Cache."+key)=data;
Application.UnLock();
},
remove:function(key){
Application.Lock();
Application.StaticObjects("xbsCache").Remove("Cache."+key);
Application.UnLock();
},
clear:function(){
Application.Lock();
Application.StaticObjects("xbsCache").RemoveAll();
Application.UnLock();
}
}
</script>
以上就是【在javascript中使用緩存技術(shù)的實(shí)際代碼】的全部?jī)?nèi)容了,歡迎留言評(píng)論進(jìn)行交流!