Smarty在数据库中缓存站点属性 [英] Smarty cache site properties in database

查看:46
本文介绍了Smarty在数据库中缓存站点属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立一个网站,该网站在数据库中具有动态内容(如标题,URL等属性).我想每次查询所有内容并分配变量都非常不必要,所以我已经了解了缓存.

I am going to build a site that have dynamic content (properties as title, url, etc) in the database. I guess it would be very unneccerary to query everything out and assign the variables, everytime, so I've read about caching.

我使用Smarty模板系统.

I use Smarty template, system.

   include('libs/Smarty/Smarty.class.php');

    $smarty = new Smarty;
    $smarty->setCaching(true);

    if (!$smarty->isCached('index.tpl')) {

    //query here and assign..
    $smarty->assign('title', 'Test');
    }

    $smarty->display('themes/simple/index.tpl');

  1. 上面的代码何时重新检查缓存?如果我要更改数据库站点属性,则希望立即更改我的站点,这可能是非常重要的事情,例如将站点进行维护等.
  2. 我真的需要在每次页面加载时一次又一次地查询所有数据以获取站点信息吗,还是有什么解决方案,例如使用缓存检查数据库行结构等等?

解决方案,我需要!

更新:我尝试了clearCache的方法,但是它似乎无法正常工作.我更新了数据库,但是似乎没有触发"clearCache"方法.

UPDATE: I try the method of clearCache, but it doesn't seem to work properly. I update the database, but the "clearCache" method, doesn't seem to be triggered, or something.

$smarty = new Smarty;
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

//just to test
$smarty->clearCache('index.tpl');


if (!$smarty->isCached('index.tpl')) {



//do stuff here

推荐答案

在修改数据库中的内容时,通知Smarty清除相关的缓存clearCache().

When modifying stuff in the database, inform Smarty to purge the relevant caches clearCache().

您还可以对照存储在数据库中的某个时间戳检查缓存的修改时间.

You're also able to check the cache's modification time against some timestamp you stored in your database.

<?php
$smarty = new Smarty();
$tpl = $smarty->createTemplate($template_file, $cache_id);
$databaseLastModified = strtotime("yesterday"); // determine when the database was modified last
if (!$tpl->isCached() || $tpl->cached->timestamp < $databaseLastModified) {
  $tpl->force_cache = true; // in case of timestamp < modified
  // load data for smarty to process
  $tpl->assign('your', 'stuff');
}
$tpl->display();

这篇关于Smarty在数据库中缓存站点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆