smarty之缓存笔记
smarty缓存技术
在smarty中,缓存分为:普通缓存,单模版都缓存,局部缓存。
缓存:
1:首选打开缓存配置项:$smarty->caching=true;
2:缓存生命周期的配置选项:$smarty->cache_lifetime=整秒数
3:$smarty->is_cached('index.html')判断index.html页面是否缓存,如果缓存,避免io操作
include('./libs/Smarty.class.php');
$smarty=new Smarty();
$conn=mysql_connect('127.0.0.1','root','');
mysql_query('use smarty_cache',$conn);
mysql_query('set nemes gbk',$conn);
$smarty->caching=true;
//开启缓存
$id=$_GET['id']+0;
$sql='select * from user where id='.$id;
if(!$smarty->is_cached('index.html'))
{
$result=mysql_query($sql,$conn);
$row=mysql_fetch_assoc($result);
$smarty->assign('id',$row['id']);
$smarty->assign('name',$row['name']);
echo '走了数据库';
}
$smarty->display('index.html');
单模版,多缓存
1:缓存参数$smarty->display('index.html',$id);
2:$smarty->is_cached('index.html',$id)判断index.html页面是否缓存,通过id来判断,id可以通过拼凑,可以参考ecshop中的缓存。
原则:凡是能影响页面内容的变量,就要根据此变量cached_id
include('./libs/Smarty.class.php');
$smarty=new Smarty();
$conn=mysql_connect('127.0.0.1','root','');
mysql_query('use smarty_cache',$conn);
mysql_query('set nemes gbk',$conn);
$smarty->caching=true;
//开启缓存
$id=$_GET['id']+0;
$sql='select * from user where id='.$id;
$cached_id=$id+'XX'
if(!$smarty->is_cached('index.html',$cached_id))
{
$result=mysql_query($sql,$conn);
$row=mysql_fetch_assoc($result);
$smarty->assign('id',$row['id']);
$smarty->assign('name',$row['name']);
echo '走了数据库';
}
$smarty->display('index.html',$cached_id);
3:局部缓存:
在模版中不需要缓存的地方,用{insert name='nocachedid'}来表示,那么该处最终的值就是,insert_nocachedid()函数的返回值
<body>
这是这是一个smarty缓存技术的页面页面<br/>
{insert name=getname}
编号:{$id} 姓名:{$name} </body>
include('./libs/Smarty.class.php');
$smarty=new Smarty();
$conn=mysql_connect('127.0.0.1','root','');
mysql_query('use smarty_cache',$conn);
mysql_query('set nemes gbk',$conn);
function insert_getname()
{
return $_SESSION['name']
}
$smarty->caching=true;
//开启缓存
$id=$_GET['id']+0;
$sql='select * from user where id='.$id;
if(!$smarty->is_cached('index.html',$id))
{
$result=mysql_query($sql,$conn);
$row=mysql_fetch_assoc($result);
$smarty->assign('id',$row['id']);
$smarty->assign('name',$row['name']);
echo '走了数据库';
}
$smarty->display('index.html',$id);
在ecshop中,用到了smarty模版技术,只能复习下smarty技术了
smarty之缓存笔记的更多相关文章
- smarty详细使用教程(韩顺平smarty模板技术笔记)
MVC是一种开发模式,强调数据的输入.处理.显示是强制分离的 Smarty使用教程1.如何配置我们的smarty解压后把libs文件夹放在网站第一级目录下,然后创建两个文件夹templates 存放模 ...
- Smarty Caching缓存 ¥ 我要打赏
Smarty Caching缓存 https://www.yiibai.com/smarty/smarty_caching.html https://www.w3cschool.cn/smarty/s ...
- smarty之缓存机制
当smarty开启缓存时,当tpl文件内容改变时,则缓存页面会重新生成 test.php: <?php date_default_timezone_set('asia/shanghai'); h ...
- smarty实现缓存
首先需要在mySmarty中添加配置信息,开启缓存,设置缓存文件存放目录,设置缓存时间缓存可以实现减少访问数据库,减轻数据库压力,访问一次数据库,形成静态页面,下次直接调用这个页面,也可以用nocac ...
- golang实现分布式缓存笔记(一)基于http的缓存服务
目录 前言 cache 缓存服务接口 cache包实现 golang http包使用介绍 hello.go Redirect.go http-cache-server 实现 cacheHandler ...
- smarty学习——缓存
存被用来保存一个文档的输出从而加速display()或fetch()函数的执行.如果一个函数被加进缓存,那么实际输出的内容将用缓存来代替. 缓存可让事物非常快速的执行,特别是带有长计算时间的模板.一旦 ...
- smarty静态缓存
缓存能让程序访问起来更加快速,调数据库的数量变少,不能实时的跟数据库同步, 一般缓存文件都放在smarty文件下cach文件夹中: 建立缓存的PHP和HTML文件: 先编辑PHP文件来查询显示数据库当 ...
- 12月18日Smarty文件缓存
缓存 做缓存的目的是为了让程序运行起来更加迅速.因为如果程序访问数据库时数据量较大,执行起来会比较慢.而且每一次刷新页面都会访问依稀数据库,然后再把数据显示在页面上. 设置缓存也有一个缺点,那就是缓存 ...
- smarty 内存缓存
<?php //缓存 //定义一个该页面的缓存文件路径 $filename="../cache/mainhc.html"; //设置一个缓存时间 $time=; //判断缓存 ...
随机推荐
- JavaMail简单接收邮件
一个简单的例子,收取所有邮件并在控制台输出. package cn.jmail.test; import java.io.*; import java.util.*; import javax.mai ...
- 深入浅析mysql引擎
mysql引擎 mysql数据库引擎取决于mysql在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译mysql.在缺省情况下,mysql支持三个引擎:ISAM,MYISAM和HEAP.另 ...
- ORACLE CASE函数 .
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- nodejs iconfont处理
做前端优化,iconfont可以替换掉很多图片,减少请求,并有很好的兼容性,颜色大小也有很好的自由度.现在网上已经有很多公开的iconfont供我们使用.但是每个项目有不同的应用场景,网上的并不能满足 ...
- 从苹果的appstore谈谈web前端那丝毫的追求
献上链接:点击进入itunes打开页面,我们先找到App 的logo图比如这个图很简单的一个图标,估计多数人选择的是上传一张处理好圆角,border的图片作为app logo,但问题是苹果觉得,你们每 ...
- 使用pymysql连接MySql数据库
MySQLdb安装失败了,直接使用pymysql,安装了pymysql. 并学习了使用使用pymysql创建数据库和表,并插入数据. __author__ = 'Administrator' impo ...
- 引用JS表单验证大全 以后方便查看用
1:js 字符串长度限制.判断字符长度 .js限制输入.限制不能输入.textarea 长度限制 2.:js判断汉字.判断是否汉字 .只能输入汉字 3:js判断是否输入英文.只能输入英文 4:js只能 ...
- 1分钟快速生成用于网页内容提取的xslt
1,项目背景 在<Python即时网络爬虫项目说明>一文我们说过要做一个通用的网络爬虫,而且能节省程序员大半的时间,而焦点问题就是提取器使用的抓取规则需要快速生成.在python使 ...
- Leetcode 283 Move Zeroes python
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- c++中在一个类中定义另一个只有带参数构造函数的类的对象
c++中在一个类中定义另一个只有带参数构造函数的类的对象,编译通不过 #include<iostream> using namespace std; class A { public: ...