+ -
当前位置:首页 → 问答吧 → SMARTY局部缓存

SMARTY局部缓存

时间:2010-07-18

来源:互联网

我做了个新闻列表,但是有个点击量是不需要缓存的,所以我使用了SMARTY的局部缓存功能,但是刷新1下点击量就没了,青大家帮忙下,代码如下
PHP文件
<?php
$Smarty = new Smarty();
$Smarty ->caching = true;
$Smarty ->cache_lifetime = 30;
$Smarty ->cache_dir = './library/Smarty/cache';
$Smarty ->compile_dir = './library/Smarty/templates_c/';
$Smarty ->template_dir = './templates/index';

function smarty_block_nocache($param, $content, &$Smarty){
  return $content;
}
$Smarty->register_block('nocache', 'smarty_block_nocache', false);
$aa=array([0]=>array([title]=>"a",[hit]=>"1"),[1]=>array([title]=>"b",[hit]=>"2"));
$Smarty->assign("news",$aa);
$Smarty->display('index.html');
?>

index文件
<{foreach from=$news item=news}>
  <h2>标题:<{$news.title}></h2>
  <h5>浏览: <{nocache}><{$news.hit}><{/nocache}></h5>
<{/foreach}>

为什么刷新下浏览就没有值了呢··请帮忙!

作者: jekom   发布时间: 2010-07-18

你查下缓存文件就知道了,

我估计:foreach上面的都缓存了,第二次并没有news.hit这个变量

作者: jlzan1314   发布时间: 2010-07-18

<{nocache}>
<{foreach from=$news item=news}>
  <h2>标题:<{$news.title}></h2>
  <h5>浏览: <{$news.hit}></h5>
<{/foreach}>
<{/nocache}>
试试.

作者: jlzan1314   发布时间: 2010-07-18

楼上这方法可行,但是,我新闻列表想把他缓存,就只要浏览这项不缓存,你这样写,整个新闻列表不就没缓存了吗?
恩,我检查了下第二次news.hit这个变量确实没值,我就搞不懂为什么会没值

作者: jekom   发布时间: 2010-07-18

友情帮定下

作者: yanwen249   发布时间: 2010-07-19

顶下,快来人解决啊

作者: jekom   发布时间: 2010-07-22

保持关注~

作者: jimoxly1103   发布时间: 2010-07-23

关注。。。

作者: lwmhy   发布时间: 2010-07-23

2lou

作者: woiskobe   发布时间: 2010-07-23

帖子沉了啊!

作者: jekom   发布时间: 2010-08-02

$aa=array([0]=>array([title]=>"a",[hit]=>"1"),[1]=>array([title]=>"b",[hit]=>"2"));
------------------

这是什么数组 ? php的语法写数组是这样的吗? 还是楼主把浏览器打印出来的直接贴上去?

PHP code
$aa = array(
   array(
   'title' => 'a',
   'hit'   => '1'
   )
);

//换成这样,有值吗?

<{foreach from=$news item=new}>
  <h2>标题:<{$new.title}></h2>
  <h5>浏览: <{nocache}><{$new.hit}><{/nocache}></h5>
<{/foreach}>


作者: coolesting   发布时间: 2010-08-02

上面模板把参数换一下,变成

from=$news item=old

作者: coolesting   发布时间: 2010-08-02

我是直接把浏览器的值贴上去的`不好意思``但是改成这样,from=$news item=old ,就直接报错了啊

作者: jekom   发布时间: 2010-08-04

$aa = array(
  array(
  'title' => 'a',
  'hit' => '1'
  )
);

//换成这样,有值吗?

<{foreach from=$news item=new}>
  <h2>标题:<{$new.title}></h2>
  <h5>浏览: <{nocache}><{$new.hit}><{/nocache}></h5>
<{/foreach}>

====================================
这样还真有值``

作者: jekom   发布时间: 2010-08-04

第一次载入页面,链接数据库获取到了点击量 news.hit 载入页面完成后smarty会创建缓存文件,这是缓存文件中并没有news.hit的值,因为他没缓存对吧, 这时刷新页面,重新载入页面,smarty会判断是否有缓存,判断有缓存直接去读取缓存,所以跳过了连接数据库这步,所以读取不到news.hit,如果想实现,就得在判断有缓存的时候任然去执行查询输出点击数

作者: hzg0403   发布时间: 2011-12-21

热门下载

更多