·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP网站建设 >> 一个asp缓存函数,支持字符串和数组

一个asp缓存函数,支持字符串和数组

作者:佚名      ASP网站建设编辑:admin      更新时间:2022-07-23

主函数
 程序代码
<%
'***********************************************
'函数名:getcache
'作  用:将需要缓存的内容,置入缓存中,并读取出来,如果缓存中存在该内容,则直接从缓存读取!
'作  者: 静¢脉(hayden)
'时  间: 2007-12-21
'参  数:funsname  ----    需要缓存的内容
'       isreset ---- 是否更新[值:0(根据时间或判断缓存为空时自动更新)、1(主动更新)]
'       isarr  ---- 所缓存的内容是否为一个数据[0为字符串,1为数组]
'       timeinfo   ---- 缓存更新时间,单位为秒,当值为0时,则只在缓存为空时,才更新
'返回值:缓存名为"funsname”的内容
'***********************************************
Function getcache(funsname,isreset,isarr,timeinfo)
    dim domain = "myhhe.cn"    '缓存域
    Dim temp_getconfig
    Dim re_getcache : re_getcache = False
    Dim temp_isarray_type : temp_isarray_type = False
    Dim Appfunsname : Appfunsname = Replace(Replace(Replace(funsname,"(",""),")",""),",",".")
    If isarr = 1 Then temp_isarray_type = True
    If isreset = 1 Then re_getcache = True
    If isreset = 2 Then
        execute("temp_getconfig="&funsname)
        getcache = temp_getconfig
        Exit Function
    End If
    If application(domain&"_"&Appfunsname&"_time") = "" And timeinfo<>0 Then re_getcache = True
    If Not re_getcache Then
        If temp_isarray_type Then
         If Not IsArray(Application(domain&"_"&Appfunsname)) Then re_getcache = True
        Else
            If Application(domain&"_"&Appfunsname) = "" Then re_getcache = True
        End If
    End If
    If Not re_getcache And timeinfo<>0 Then
        If Int(DateDiff("s",Application(domain&"_"&Appfunsname&"_time"),now()))>timeinfo Then re_getcache = True
    End If
    If re_getcache Then
        execute("temp_getconfig="&funsname)
        Application.Lock
        Application(domain&"_"&Appfunsname) = temp_getconfig
        Application(domain&"_"&Appfunsname&"_time") = Now()
        Application.UnLock
    Else
        temp_getconfig=Application(domain&"_"&Appfunsname)
    End If
    getcache = temp_getconfig
End Function
%>


调用示例:
 程序代码
<%
Function out_test1    '返回一个字符串的示例函数
    out_test1="这里是一个字符串"
End Function

Function out_test2    '返回一个数组的示例函数
    Dim temp_out_test2
    temp_out_test2="这里.是.一个.数组"
    out_test2=Split(temp_out_test2,".")
End Function

Dim i

'字符串缓存(将函数out_test1从缓存读取并输出)
Dim str2 : str2 = getcache("out_test1",0,0,180)    '通过getcache函数读取缓存.刷新时间为180秒,(当out_test1缓存为空,会自动访问函数out_test1输出,并同时置入缓存~)
response.write str2

response.write "<BR><BR><BR>"

'数组缓存(将函数out_test2从缓存读取并输出)
Dim str1 : str1 = getcache("out_test2",0,1,180)  '同上(字符串缓存说明)
For i = 0 To UBound(str1)
    response.write str1(i) & "<BR>"
Next
%>