欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在JScript中使用緩存技術(shù)的實(shí)際代碼

 更新時(shí)間:2008年05月18日 21:29:36   作者:  
在編寫ASP程序時(shí),通常為了提高ASP程序的運(yùn)行效率及減少對(duì)數(shù)據(jù)庫(kù)的連接和查詢,會(huì)使用緩存技術(shù)來緩存一些需要從數(shù)據(jù)庫(kù)讀取的數(shù)據(jù)。而在ASP中實(shí)現(xiàn)緩存的方法常用的就是使用Application對(duì)象。在編寫ASP程序時(shí),我們有兩種語(yǔ)言可以選擇,分別是VBScript和JScript。
在使用VBScript時(shí),我們可以用Application緩存數(shù)組來實(shí)現(xiàn)緩存,例:

程序代碼:
復(fù)制代碼 代碼如下:

Dim rs,arr 
rs.Open conn,sql,1,1 
arr=rs.GetRows() 
Application.Lock() 
Application("cache")=arr 
Applicatoin.UnLock() 

在VBScript里,數(shù)組是可以存到Application對(duì)象里的,但是如果ASP的語(yǔ)言選擇為JScript的話,那么就有些不妙了,我們?cè)谑褂肁pplication儲(chǔ)存一個(gè)數(shù)組時(shí),會(huì)出現(xiàn)以下錯(cuò)誤:

引用內(nèi)容:
Application object, ASP 0197 (0x80004005)

Disallowed object use

Cannot add object with apartment model behavior to the application intrinsic object.

在微軟的知識(shí)庫(kù)可以找到具體原因如下:

引用內(nèi)容:
JScript arrays are considered to be "Apartment" COM components. Only Component Object Model (COM) components that aggregate the Free Threaded Marshaler (FTM) can be assigned to Application scope within an Internet Information Server (IIS) 5.0 ASP page. Because an "Apartment" component cannot aggregate the FTM (it cannot allow a direct pointer to be passed to its clients, unlike a "Both with FTM" object), JScript arrays do not aggregate the FTM. Therefore, JScript arrays cannot be assigned to Application scope from an ASP page.

以上描述引用自:PRB: Error When You Store a JScript Array in Application Scope in IIS 5.0

因此,為了解決這個(gè)問題,在Google里找了一大會(huì),終于找到了一篇文章《Application對(duì)象的Contents和StaticObjects做Cache的一些結(jié)論》,解決了這個(gè)問題,方法就是使用Application.StaticObject存放一個(gè)Scripting.Dictionary對(duì)象,然后再使用Scripting.Dictionary對(duì)象來存放需要緩存的數(shù)據(jù)。

據(jù)此,寫了一個(gè)操作緩存的類,實(shí)現(xiàn)put、get、remove和clear方法,使用之前,需要在global.asa中添加一個(gè)object:

程序代碼:
<object id="xbsCache" runat="server" scope="Application" progid="Scripting.Dictionary"></object>
類的實(shí)現(xiàn)如下:
復(fù)制代碼 代碼如下:

<script language="JScript" runat="server"> 
/** 
 Title: cache operate class 
 Description: operate system cache 
 @Copyright: Copyright (c) 2007 
 @Author: xujiwei 
 @Website: http://www.xujiwei.cn/ 
 @Version: 1.0 
 @Time: 2007-06-29 12:03:45 
**/ 
var xbsCache = { 
    get: function(key) { 
        return Application.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> 
如此,就完成了ASP中使用JScript時(shí)的緩存實(shí)現(xiàn)。

相關(guān)文章

最新評(píng)論