Smarty 在数据库中缓存站点属性

发布于 2024-12-08 23:11:40 字数 904 浏览 0 评论 0原文

我将建立一个在数据库中包含动态内容(标题、网址等属性)的网站。我想每次都查询所有内容并分配变量是非常不必要的,所以我已经阅读了有关缓存的内容。

我使用Smarty模板、系统。

   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”方法似乎没有被触发,或者其他什么。

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

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


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



//do stuff here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵强ㄟ 2024-12-15 23:11:40

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

您还可以根据存储在数据库中的某些时间戳来检查缓存的修改时间。

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文