Smarty cache site properties in database

◇◆丶佛笑我妖孽 提交于 2019-12-12 03:06:07

问题


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.

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. The code above When does it recheck the cache? If I do a change in my database site properties, I want INSTANTLY change upon my site, can be very important stuff like put the site to maintenance, etc.
  2. Do I really need to query all data, again and again on every page load to grab the site info, or is there any solution, like checking the database row structure with the cache, or something like that?

Solutions, I need!


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

回答1:


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();


来源:https://stackoverflow.com/questions/7714946/smarty-cache-site-properties-in-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!