编者前言:
在web程序中实现缓存的方法有很多办法,例如用文件缓存,把某些数据临时的放入文件中;或者在数据库中缓存需要大量读取的数值;另外的较常见的方法就是利用全局变量进行缓存。在asp中利用application实现数据的缓存是很常见的,可以把某些共有的数据从数据库中独立出来,从而减少服务器的压力。缓存会占用一定的内存,所以不可用application存储很大的数据。
/>
我认为在ASP中最好的办法是用编程实现定时刷新Cache,也就是说给Application中储存的设一个过期时间。当然,在ASP中Application对象没有这样一个ExpireTime属性。这需要用程序实现。
以下为引用的内容: Code ASP:default.asp
<%@Language=VBScript%> <%Option Explicit%> <%Response.Buffer=True%> <!--#include file = "conn.asp"--> <!--#include file = "GetCache.asp"--> <HTML> <HEAD> <TITLE>ASP Cache演示</TITLE> <META HTTP-EQUIV="Content-Type" C> </HEAD> <BODY> <h4>每隔10秒刷新Cache:</h4> <% response.Flush GetHTMLStream response.Write HTMLStream %> </body> </html>
|
ASP:getcache.asp
以下为引用的内容: <% Const CACHE_DEFAULT_INTERVAL = 30 '每隔30秒刷新一次cache Dim HTMLStream Dim IsExpires IsExpires = CacheExpires Function CacheExpires Dim strLastUpdate Dim result strLastUpdate = Application("LastUpdate") If (strLastUpdate = "") Or (CACHE_DEFAULT_INTERVAL < DateDiff("s", strLastUpdate, Now)) Then result = true SetLastUpdateTime Else result = false End If CacheExpires = result End Function %> |
上面是一个最简单的例子。