26
2019.2
判断缓存
作者: POPASP
判断是否被缓存,使用isCached方法。
| 项目 | 说明 |
| --- | --- |
| 参数 | 字符串"控制器/模板"或者是array( "控制器/模板" , label ),label指给页面分配一个缓存标识符,如果为空,则取"控制器/方法" |
| 返回值 | 如果处于缓存期返回True,否则返回False |
举例
控制器文件index.asp,代码如下:
```brush:vb
<%
Class Index
sub cache
dim label
label = POP_MVC.get("id")
if not that.isCached( array( "" , label ) ) then
that.d("cacheTime") = now()
end if
that.display("")
end sub
End Class
%>
```
模板文件Tpl/Index/cache.html,内容仅为一行
```brush:html
{$cacheTime}
```
访问
```brush:html
http://serverName/index.asp?c=index&a=cache&id=1
```
并多次刷新,我们会发现得到的是当前时间,没有进行任何缓存。缓存文件为`在Runtime/Cache/Index_Cache1.html`,打开这个文件,可以看到缓存的时间。
访问
```brush:html
http://serverName/index.asp?c=index&a=cache&id=2
```
则会根据id,生成缓存文件`在Runtime/Cache/Index_Cache2.html`。
在文章页面,我们可以将文章ID作为缓存标识,在分类页面,我们可以将分类ID与页码page作为缓存标识,比如
```brush:vb
sub cache
dim label
label = POP_MVC.get("id") & "_" & POP_MVC.get("page")
if not that.isCached( array( "" , id ) ) then
that.d("cacheTime") = now()
end if
end sub
```