网络编程
位置:首页>> 网络编程>> Asp编程>> 最近写的一个asp缓存函数

最近写的一个asp缓存函数

作者:hayden 来源:烦恼's BLOG 发布时间:2008-11-25 14:07:00 

标签:缓存,函数,asp

关于asp缓存函数,类什么的,在网上可以说笔笔皆是,为啥我要不辞辛苦去写一个呢?

大概看了下,各有各的优点吧,可是大部分好像不可以缓存数据额,还得把数据用一定的符号相隔转成一个变量去缓存,取出来用的时候还要split转成数组,有些麻烦了,所以这就重写了一个,也给大家分享分享,有什么更好的想法,欢迎来“稿”!谢谢~

 程序代码

<%
'***********************************************
'函数名:getcache
'作  用:将需要缓存的内容,置入缓存中,并读取出来,如果缓存中存在该内容,则直接从缓存读取!
'作  者: 静¢脉(hayden) 
'时  间: 2007-12-21
'参  数:funsname  ----    需要缓存的内容
'       isreset ---- 是否更新[值:0(根据时间或判断缓存为空时自动更新)、1(主动更新)]
'       isarr  ---- 所缓存的内容是否为一个数据[0为字符串,1为数组]
'       timeinfo   ---- 缓存更新时间,单位为秒,当值为0时,则只在缓存为空时,才更新
'返回值:缓存名为"funsname”的内容
'***********************************************
Function getcache(funsname,isreset,isarr,timeinfo)
    dim domain : domain = "www.mysuc.com"    '缓存域
    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
%>
0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com