5ucms论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 6601|回复: 1

[2.x] 10、POPASP快速入门第十课 缓存技术

[复制链接]

670

主题

785

帖子

8228

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8228
发表于 2019-4-1 16:33:01 | 显示全部楼层 |阅读模式
cache.asp

  1. <!--#include file="./popasp_inc.asp"-->
  2. <%
  3. Call A_("Index/cache")

  4. dim key,item
  5. %>
  6. <html>
  7. <head>
  8. <title>数据缓存</title>
  9. </head>
  10. <body>
  11. <p>
  12. 缓存时间:<br />
  13. <%=V_("cache_time")%>
  14. </p>

  15. <p>
  16. 当前时间:<br />
  17. <%=V_("now_time")%>
  18. </p>
  19. </body>
  20. </html>
复制代码


IndexAction.class.asp
  1. <%
  2. ' 本类由系统自动生成,仅供测试用途
  3. Class IndexAction
  4.     public Sub index()
  5.                 dim rs , range , page
  6.                 '通过数据模型从数据库中取出一条记录
  7.                 'set rs = M_("post").db.where(5).field("id,title").find()               
  8.                 'var_export rs
  9.                
  10.                 if that.get("page") = "" then
  11.                         page = 1
  12.                 else
  13.                         page = that.get("page")               
  14.                 end if
  15.                
  16.                 if that.expired( "index" , page ) then
  17.                
  18.                         set rs = M_("post").db.page( array(null,10) ).field("id,title,add_time").select()        
  19.         
  20.                         that.assign "page",P_("PAGE")(rs).show()
  21.                         
  22.                         that.assign "list",array(rs)
  23.                         
  24.                         that.cache '缓存的是整个该情况页,所以不需要else,自动显示缓存结果
  25.                 end if
  26.     end Sub
  27.         
  28.         public sub post
  29.                 dim id
  30.                
  31.                 'that.get是request.querystring的一种简写
  32.                 id = that.get("id")
  33.                
  34.                 if that.expired("post",id) then
  35.                
  36.                         set rs = M_("post").db.field("id,title,add_time,content").where(id).find()
  37.                
  38.                         that.assign "row",rs
  39.                         
  40.                         that.cache
  41.                 end if
  42.         end sub
  43.         
  44.         sub cache
  45.                 'var_export that
  46.         
  47.                 'expired有两个参数,第一个tpl是指模板文件名,第二id是指唯一标识符
  48.                 if that.expired("cache","") then
  49.                         '发送当前时间
  50.                         that.assign "cache_time" , now()
  51.                         
  52.                         'cache方法是指将它前面用assign方法分配的变量缓存起来
  53.                         that.cache
  54.                         'that.assign "var", now()
  55.                 end if
  56.                
  57.                
  58.                 that.assign "now_time" , now()               
  59.         end sub
  60. End Class
  61. %>
复制代码

回复

使用道具 举报

670

主题

785

帖子

8228

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8228
 楼主| 发表于 2019-4-1 16:38:19 | 显示全部楼层
缓存要点:
1、判断缓存是否过期,用that.expired
2、默认缓存时间3600秒,即1小时可以通过config.asp文件重定义时间,call C_( Array("TMPL_CACHE_LIFETIME" , 1 )) 这样就1秒了
3、修改控制器文件缓存失效
4、缓存数据使用that.cache方法
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|5ucms.com

GMT+8, 2024-4-19 09:25 , Processed in 0.953125 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表