缓存的相关知识

缓存:
1.什么是缓存(cache)?
内存(临时存放数据,解决cpu和外部设备之间速度不匹配)
高速缓存(cache)(解决cpu和内存之间速度不匹配的问题)
作用:提高计算机数据的访问速度
网站设计中(软件开发中)
缓存的概念:在PHP中,缓存就是就是硬盘中的一块区域,将生成的不变的页面放入这块区域中,相当于直接放入了缓存。当用户重复访问相同页面的时候,直接从缓存区域调用页面。
2.如何应用缓存(smarty中)
(1)单页面单缓存(建立缓存)
步骤1:开启缓存($smarty->caching=true|$smarty->caching=2设置lifetime)
步骤2:设置缓存目录($smarty->cache_dir='')
步骤3:设置缓存文件的生存时间($smarty->cache_lifetime)
步骤4:利用display()或fetch()生成缓存文件

(2).单页面 多缓存
为了处理同样的显示界面,不同的显示内容。内容有url参数决定。同一个模板可以生成多个缓存文件(有多少个内容显示就有多少个缓存文件),多个缓存缓存文件用display("*tpl",$_GET["id"]);在PHP中可以用is_cached判断模板是否被缓存,is_cached格式和display完全一样。
(3)局部不缓存
页面很复杂,需要设置缓存。但是页面上包含实时性较强的数据(时间,),所以在这样的页面上我们将实时性较强的数据部分设置缓存,将其他的几乎不变的内容加上缓存。

简述:
is_cached (是否已被缓存)

void is_cached (string template, [string cache_id])


This returns true if there is a valid cache for this template. This only works if caching is set to true.


在指定模板的缓存存在是返回真。只有在缓存设置为真时才可用。

 

例子 13-18. 是否已被缓存


$smarty->caching = true;
if(!$smarty->is_cached("index.tpl")) { // do database calls, assign vars here // 调用数据库,并对变量进行赋值}
$smarty->display("index.tpl");
 
 
//局部不缓存:

insert


属性     类型   是否必须    缺省值        描述
name    string     Yes        n/a    插入函数的名称
assign  string     No         n/a    该属性指定一个变量保存待插入函数输出
script  string     No         n/a    插入函数前需要先包含的php脚本名称
 
[var ...] [var type] No       n/a    传递给待插入函数的本地参数


Insert tags work much like include tags, except that insert tags are not cached when you have template caching enabled. They will be executed on every invocation of the template.

Insert 函数类似欲 inluce 函数,不同之处是 insert 所包含的内容不会被缓存,每次调用该模板都会重新执行该函数.

Let's say you have a template with a banner slot at the top of the page. The banner can contain any mixture of HTML, images, flash, etc. so we can't just use a static link here, and we don't want this contents cached with the page. In comes the insert tag: the template knows #banner_location_id# and #site_id# values (gathered from a config file), and needs to call a function to get the banner contents.

例如你在页面上端使用一个带有广告条位置的模板,广告条可以包含任何HTML、图象、FLASH等混合信息. 因此这里不能使用一个静态的链接,同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定:#banner_location_id# 和 #site_id# 值(从配置文件中取),同时需要一个函数取广告条的内容信息.

Example 7-10. function insert
例 7-10. insert 函数演示

{* example of fetching a banner *}
{insert name="getBanner" lid=#banner_location_id# sid=#site_id#}
 
 

In this example, we are using the name "getBanner" and passing the parameters #banner_location_id# and #site_id#. Smarty will look for a function named insert_getBanner() in your PHP application, passing the values of #banner_location_id# and #site_id# as the first argument in an associative array. All insert function names in your application must be prepended with "insert_" to remedy possible function name-space conflicts. Your insert_getBanner() function should do something with the passed values and return the results. These results are then displayed in the template in place of the insert tag. In this example, Smarty would call this function: insert_getBanner(array("lid" => "12345","sid" => "67890")); and display the returned results in place of the insert tag.

在此例中,我们使用了 getBanner 作为 name 属性,同时传递了 #banner_location_id# 和 #site_id# 两个参数. 接下来 Smarty 在你的 php 程序中搜索名为 insert_getBanner() 的函数,#banner_location_id# 和 #site_id# 的值被组合成一个数组作为函数的第一个参数传递给该函数. 为了避免函数命名混乱,所有的 insert 函数都必须以 insert_ 开头. 你的 insert_getBanner() 函数根据传递的参数执行并返回执行的结果. 这些结果就显示在模板中调用该函数的位置. 在此例中 Smarty 调用该函数类似insert_getBanner(array("lid"=>"12345","sid"=>67890"));并将返回的结果显示在调用的位置.


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值