南强小屋 Design By 杰米
在使用VBScript时,我们可以用Application缓存数组来实现缓存,例:
程序代码:
复制代码 代码如下:
Dim rs,arr
rs.Open conn,sql,1,1
arr=rs.GetRows()
Application.Lock()
Application("cache")=arr
Applicatoin.UnLock()
在VBScript里,数组是可以存到Application对象里的,但是如果ASP的语言选择为JScript的话,那么就有些不妙了,我们在使用Application储存一个数组时,会出现以下错误:
引用内容:
Application object, ASP 0197 (0x80004005)
Disallowed object use
Cannot add object with apartment model behavior to the application intrinsic object.
在微软的知识库可以找到具体原因如下:
引用内容:
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
因此,为了解决这个问题,在Google里找了一大会,终于找到了一篇文章《Application对象的Contents和StaticObjects做Cache的一些结论》,解决了这个问题,方法就是使用Application.StaticObject存放一个Scripting.Dictionary对象,然后再使用Scripting.Dictionary对象来存放需要缓存的数据。
据此,写了一个操作缓存的类,实现put、get、remove和clear方法,使用之前,需要在global.asa中添加一个object:
程序代码:
<object id="xbsCache" runat="server" scope="Application" progid="Scripting.Dictionary"></object>
类的实现如下:
复制代码 代码如下:
<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时的缓存实现。
程序代码:
复制代码 代码如下:
Dim rs,arr
rs.Open conn,sql,1,1
arr=rs.GetRows()
Application.Lock()
Application("cache")=arr
Applicatoin.UnLock()
在VBScript里,数组是可以存到Application对象里的,但是如果ASP的语言选择为JScript的话,那么就有些不妙了,我们在使用Application储存一个数组时,会出现以下错误:
引用内容:
Application object, ASP 0197 (0x80004005)
Disallowed object use
Cannot add object with apartment model behavior to the application intrinsic object.
在微软的知识库可以找到具体原因如下:
引用内容:
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
因此,为了解决这个问题,在Google里找了一大会,终于找到了一篇文章《Application对象的Contents和StaticObjects做Cache的一些结论》,解决了这个问题,方法就是使用Application.StaticObject存放一个Scripting.Dictionary对象,然后再使用Scripting.Dictionary对象来存放需要缓存的数据。
据此,写了一个操作缓存的类,实现put、get、remove和clear方法,使用之前,需要在global.asa中添加一个object:
程序代码:
<object id="xbsCache" runat="server" scope="Application" progid="Scripting.Dictionary"></object>
类的实现如下:
复制代码 代码如下:
<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时的缓存实现。
标签:
JScript,缓存技术
南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米
暂无在JScript中使用缓存技术的实际代码的评论...