zoukankan      html  css  js  c++  java
  • smarty缓存

    smarty缓存 实际上是将解析完的数据保存成一个静态的网页

    //静态缓存
    //使访问更加快速,减少对数据库的操作量,提示用户体验 减小服务器压力
    //原理:PHP文件执行完之后,将解析完的数据保存成一个静态的网页 再次访问时 直接访问静态网页 无需再次访问数据库
    //缺点:不能实时的跟数据库同步 在缓存设定时间内 静态网页无法更改
    //适用范围限于一些小程序 对时间精度要求较高的 例如 抢购 无法满足

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>无标题文档</title>
     6 </head>
     7 
     8 <body>
     9 <h1>数据列表</h1>
    10 <table border="1" cellpadding="0" cellspacing="0" width="100%">
    11     <tr>
    12         <td>代号</td>
    13         <td>名称</td>
    14         <td>操作</td>
    15     </tr>
    16     <{foreach $shuzu as $v}>
    17     <tr>
    18           <td><{$v[0]}></td>
    19         <td><{$v[1]}></td>
    20         <td><a href="shanchu.php?code=<{$v[0]}>">删除</a>
    21              <a href="xiugai.php?code=<{$v[0]}>">修改</a>
    22         </td>
    23     </tr>
    24     
    25     <{/foreach}>
    26     
    27     
    28 </table>
    29 <div><{$fpage}></div>
    30 
    31 </body>
    32 </html>
    huancun.html

    访问主页面 huancun.php

     1 <?php
     2 //静态缓存 
     3 //使访问更加快速,减少对数据库的操作量,提示用户体验  减小服务器压力
     4 //原理:PHP文件执行完之后,将解析完的数据保存成一个静态的网页  再次访问时 直接访问静态网页 无需再次访问数据库
     5 //缺点:不能实时的跟数据库同步  在缓存设定时间内 静态网页无法更改 
     6 //适用范围限于一些小程序 对时间精度要求较高的  例如 抢购 无法满足
     7 $p = 1;
     8 if(!empty($_GET["page"]))
     9 {
    10     $p = $_GET["page"];
    11 }
    12  
    13 $filename = "../cache/huancun{$p}.html";//缓存文件存放的位置
    14 $time = 10;//缓存有效期 10 秒
    15 
    16 //判断缓存文件是否存在,如果缓存文件存在直接调用缓存,如果缓存文件不存在重新缓存
    17 if(file_exists($filename) && ((filemtime($filename)+$time)>= time()) )                     
    18 //file_exists()判断文件是否存在
    19 {
    20     //直接调用缓存
    21     include($filename);
    22     
    23 }
    24 else
    25 {
    26     //重新缓存
    27     ob_start(); //开启内存缓存
    28     
    29     include("../init.inc.php");
    30     include("../DBDA.php");
    31     $db = new DBDA();
    32     $sall = "select count(*) from nation";
    33     $zts = $db->StrQuery($sall);
    34     include("../page.class.php");
    35     $page = new Page($zts,5);
    36     
    37     $sql = "select * from nation ".$page->limit;
    38     $arr = $db->Query($sql);
    39     
    40     $smarty->assign("fpage",$page->fpage());
    41     $smarty->assign("shuzu",$arr);
    42     $smarty->display("huancun.html");
    43     
    44     $str = ob_get_contents(); //获取内存中的缓存内容
    45     file_put_contents($filename,$str);
    46     
    47     ob_flush(); //关闭内存缓存
    48     echo"重新缓存#############*****************%%%%%%%%%%$$$$$$$@@@@@@@@";
    49 }
    huancun.php

    同时练习了一下 删除和修改

    删除:

     1 <?php
     2 
     3 include("../DBDA.class.php");
     4 $db = new DBDA();
     5 
     6 $code = $_GET["code"];
     7 
     8 $sql = "delete from nation where code='{$code}'";
     9 $r = $db->Query($sql,0);
    10 if($r)
    11 {
    12     header("location:huancun.php");
    13 }
    14 else
    15 {
    16     "删除失败!";
    17     
    18 }
    shanchu.php

    修改:

    1 <?php
    2 include("../init.inc.php");
    3 include("../DBDA.php");
    4 $db = new DBDA();
    5 $code = $_GET["code"];
    6 $sql = "select * from nation where code='{$code}' ";
    7 $arr = $db->Query($sql);
    8 $smarty->assign("nation",$arr[0]);
    9 $smarty->display("xiugai.html");
    xiugai.php
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>无标题文档</title>
     6 </head>
     7 
     8 <body>
     9 <h1>修改页面</h1>
    10 <form action="update.php" method="post">
    11 <div><input hidden="hidden" type="text"  name="code" value="<{$nation[0]}>"/></div>
    12 <div>名称:<input type="text"  name="name" value="<{$nation[1]}>"/></div>
    13 <input  type="submit" value="修改"/>
    14 </form>
    15 </body>
    16 </html>
    xiugai.html
     1 <?php
     2 include("../DBDA.class.php");
     3 $db = new DBDA();
     4 $code = $_POST["code"]; 
     5 echo $code;
     6 $name = $_POST["name"];
     7 $sql = " update  nation set name = '{$name}' where code='{$code}' ";
     8 $arr = $db->Query($sql,0);
     9 if($arr)
    10 {
    11     header("location:huancun.php");
    12     
    13 }
    14 else
    15 {
    16     echo "修改失败!";
    17 }
    update.php

  • 相关阅读:
    sp_executesql 带输出参数的 EXEC 动态执行 (z)
    查端口是否被占用 DOS 命令netstat(Z)
    MS SQL处理树型结构数据 将子节点记录的各项字段值累加赋值给其父节点
    lazarus unidac使用注意事项
    UNIDAC在arm linux运行出错的解决方法
    [转]为Linux下的Lazarus添加中文输入支持
    银河麒麟(aarch64)安装Lazarus步骤
    raspberry 添加拼音五笔输入法
    lazarus跨平台编译步骤
    拷贝构造函数与赋值操作符的具体实现 【转】参考度4.5星
  • 原文地址:https://www.cnblogs.com/bhmmlxieliming/p/6517250.html
Copyright © 2011-2022 走看看