When using VBScript, we can use application cache arrays to implement caching, for example:
Program code:
Copy Code code as follows:
Dim Rs,arr
Rs. Open conn,sql,1,1
Arr=rs. GetRows ()
Application.Lock ()
Application ("Cache") =arr
Applicatoin.unlock ()
In VBScript, arrays can be stored in application objects, but if the ASP's language is selected as JScript, then it's not good, and when we use application to store an array, the following error occurs:
Reference content:
Application object, ASP 0197 (0x80004005)
Disallowed object use
Cannot add object with apartment model behavior to the application intrinsic object.
The Microsoft Knowledgebase can be found for specific reasons as follows:
Reference content:
JScript arrays are considered to be "Apartment" COM components. Only Component Object Model (COM) components that aggregate-free threaded marshaler (FTM) can is assigned to Applicati On 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 is passed to it clients, u Nlike a "Both with FTM" object), JScript arrays does not aggregate the FTM. Therefore, JScript arrays cannot is assigned to application scope from a ASP page.
The above description references from: Prb:error when you Store a JScript Array in application Scope in IIS 5.0
Therefore, in order to solve this problem, in Google find a conference, finally found an article "Application object contents and StaticObjects to do some of the cache conclusions," solve this problem, The method is to use Application.staticobject to store a Scripting.Dictionary object and then use the Scripting.Dictionary object to store the data that needs to be cached.
Thus, a class that operates a cache is written to implement the put, get, remove, and clear methods, and you need to add an object to the Global.asa before you use it:
Program code:
<object id= "Xbscache" runat= "Server" scope= "Application" progid= "Scripting.Dictionary" ></object>
The implementation of the class is as follows:
Copy Code code as follows:
<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>
This completes the caching implementation of the ASP in which JScript is used.