WIXOO

Digest

KingCMS模板缓存方式更改加快生成速度

作者:Ash 发布时间 09/12/25 来源 Salad UN

很早就看到过这篇文章,作者的出发点和悟空提到的方法有相同之处,cms生成速度优化实验结果(一) 经过证实,其实最大的问题应该在于数据库的效率和读取的速度的问题上,当然读取数据库的程序也应该有关,毕竟我不是专业的程序员,对数据库程序优化理解还是一知半解.

下面看看这个方法:

KingCMS原程序每生成一个栏目将读取一次IO的模板。
我将KingCMS的IO读取模板的函数转换为我自己写的缓存模板方式!

可能大家说,改了模板怎么更新相关的缓存呢?
我的函数已经写好了针对模板的增删除缓存方法。

需要改动几个文件:

第一个:打开 /page/system/fun.asp
(原理,替换原有的函数处理模式,换处理方式而不换函数的调用结构)

找到这个
public function readfile(l1)

函数
public function readfile(l1)
        dim fs,stm,l2
        set fs=createobject(king_fso)
                l2=server.mappath(l1)
                if fs.fileexists(l2) then
                        set stm=server.createobject(king_stm)
                        stm.charset=king_codepage
                        stm.open
                        stm.loadfromfile l2
                        readfile=stm.readtext
                        set stm=nothing
                end if
        set fs=nothing
end function
 

将这个函数代码替换为下面的几个新函数:
(原理作用:读取模板的时候,将判断这个模板是否存在缓存,如果不存在就第一次IO后缓存起来,以便下次读取的时候从缓存取出)

'/*** yuyuyu88.Function ***/
public function readfile(l1)
        Dim tempfile
        '这里将判断是否为模板路径,保持此函数的原有功能,如果不是模板路径,将实时IO读取
        If InStr(l1, "../../" & king_templates & "/") > 0 Then
                '如果缓存为空将读取一次硬盘的模板
                If IsEmpty(LoadCache(l1)) Then
                        tempfile = readdiskfile(l1)
                        Call InsertCache(l1, tempfile)
                Else
                        tempfile = LoadCache(l1)
                End If
        Else
                tempfile = readdiskfile(l1)
        End If
        readfile = tempfile
end function
public function readdiskfile(l1)
        dim fs,stm,l2
        set fs=createobject(king_fso)
                l2=server.mappath(l1)
                if fs.fileexists(l2) then
                        set stm=server.createobject(king_stm)
                        stm.charset=king_codepage
                        stm.open
                        stm.loadfromfile l2
                        readdiskfile=stm.readtext
                        set stm=nothing
                end if
        set fs=nothing
end function
Public Sub InsertCache(Name, Content)
        Application(Name) = Content ' & "<!--/*** Cache: " & Name & ", Time: " & Now() & " ***/-->"
End Sub
Public Function LoadCache(Name)
        Dim TempCache
        If IsEmpty(Application(Name)) Then
                TempCache = Empty
        Else
                TempCache = Application(Name)
        End If
        LoadCache = TempCache
End Function
Public Sub RemoveCache(Name)
        Dim objItem
        If Name = "ALL" Then
                For Each objItem in Application.Contents
                        Application.Contents.Remove(objItem)
                Next
        ElseIf InStr(LCase(objItem), "/template/") > 0 Then
                For Each objItem in Application.Contents
                        If InStr(LCase(objItem), "/template/") > 0 Then
                                Application.Contents.Remove(objItem)
                        End If
                Next
        Else
                If Not IsEmpty(Application(Name)) Then
                        Application.Contents.Remove(Name)
                End If
        End If
End Sub
'遍历模板文件夹并缓存
Sub CacheTemplates(Path)
        On Error Resume Next
        Dim tempurl, sitepath, i, fs, file, folder, item, folderurl, fileurl
        tempurl = "../../" & king_templates & "/"
        sitepath = server.mappath("/") & "\"
        If Path = "" Then
                Exit Sub
        ElseIf Path = "ALL" Then
                file = server.mappath(tempurl)
        Else
                file = Path
        End If
        Set fs = CreateObject(king_fso)
        Set folder = fs.GetFolder(file)
                For Each item In folder.SubFolders
                        Call CacheTemplates(item.Path)
                Next
                For Each item In folder.Files
                        folderurl = replace(folder.Path, sitepath, "")
                        folderurl = replace(folderurl, king_templates, "")
                        if right(lcase(item.path), 3) = "htm" then
                                fileurl = tempurl & folderurl & "/" & item.Name
                                fileurl = replace(fileurl, "\", "/")
                                fileurl = replace(fileurl, "//", "/")
                                Call InsertCache(fileurl, readdiskfile(fileurl))
                        end if
                    Next
        Set folder = Nothing
End Sub
'/*** yuyuyu88.Function ***/
第二步:打开 /admin/system/login.asp
(原理与作用:管理员登陆成功之后即开始缓存模板文件夹的所有htm文件,这里实现首次全部缓存。)
(如果生成的过程,缓存突然被服务器IIS进程的回收,不用担心,我的函数将判断是否存在然后就读取一次IO并缓存这个模板。上面提及到的~~~)

找到这个代码:
response.redirect "manage.asp"
替换为下面的新代码:

'读取所有模板并缓存
set king=new kingcms
        king.CacheTemplates("ALL")
set king=nothing
response.redirect "manage.asp"
至此,我们完成了针对KINGCMS的改造,生成的时候就减少IO的频繁读取了。