When writing ASP programs, in order to improve the efficiency of ASP programs and reduce connections and queries to the database, caching technology is usually used to cache some data that needs to be read from the database. Next, the wrong new technology channel will take you to take a look!
example:
Program code:
The code copy is 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 language is selected as JScript, then it will be a little bad. When we use Application to store an array, the following error will occur:
Quote:
Application object, ASP 0197 (0x80004005)
Disallowed object use
Cannot add object with apartment model behavior to the application intrinsic object.
The specific reasons can be found in Microsoft's knowledge base as follows:
Quote:
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.
The above description is quoted from: PRB: Error When You Store a JScript Array in Application Scope in IIS 5.0
Therefore, in order to solve this problem, I searched for a meeting in Google and finally found an article "Some conclusions about Cache for Application Objects and StaticObjects". To 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.
Based on this, a class for operation cache is written to implement put, get, remove and clear methods. Before using it, you need to add an object in global.asa:
Program code:
<object id="xbsCache" runat="server" scope="Application" program="Scripting.Dictionary"></object>
The implementation of the class is as follows:
The code copy is as follows:<script language="JScript" runat="server">
/**
Title: cache operating class
Description: operating 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>
In this way, the cache implementation when using JScript in ASP is completed. I hope that the content compiled by the editor of the Error New Technology Channel can provide you with help.