简体   繁体   中英

Smarty Caching (With Dynamic Content)

I have a very dynamic (social networking) site running smarty that I want to enable caching for.

My Structure: index.php display()s template.tpl template.tpl include()s indexContent.tpl

Most of the content in template.tpl is static .. such as the scripts, banner, footer.. etc. How can I cache that but not specific parts which look different to depending on whose logged in (among other factors)?

I've discovered 3 methods:

  1. {nocache} {include='indexContent.tpl'} {nocache}
  2. {dynamic} {include ...
  3. Set the cache_id for each page.

Unfortunately each has a problem:

  1. Doesn't really seem to work? Dynamic content still gets cached..
  2. Not sure how to implement or how it's different than (1)
  3. How to identify uniquely? Some pages have the same "name" but different content for specific members... think "myProfile.php"

Any suggestions? Thanks!!

You can use reverse proxy, like Varnish to cache the static part of the page and to include your dynamic content as Server-Side Includes (for Varnishi it is ESI ). Next you will need to setup the caching rules for your static and dynamic URLs so that the static one will be cached for a long time period while the dynamic one will not be cached at all.

To make it easier to understand the whole idea here is how your page HTML code could look like:

<html>
<head>...</head>
<body>
    ...some static layout...
    <esi:include src="/esi/indexContent.php"/>
    ...some another static layout...
</body>
</html>

Where /esi/indexContent.php is the script that generates the dynamic content.

For Varnish: beware of the gzipped or deflated content with ESIs as it is described in the answer here

We have the same scenario. Our entire front page is cached except for a couple of dynamic elements (news, latest forum threads) and the easiest way I found to get around this is to add in a keyword to the cached template

NEWS_BLOCK

on your logic script you then load your news template and preg_replace it with the keyword.

$news_template = $smarty->fetch('news_template.smrt');
$page_body_raw = $smarty->fetch('frontpage.smrt');
$page_body = preg_replace('/NEWS_BLOCK/', $news_template, $page_body_raw);

I know the question is old my i am still proposing a solution to help someone else.

I seem to get into same trouble with a social networking site i am developing. Here is the solution that worked for me

  1. Doesn't really seem to work? Dynamic content still gets cached..
  2. Not sure how to implement or how it's different than (1)

Just remove the static part of your page like footer and header and put them in a different tpl file. Then include the tpl file as

{include file='head.html' cache_lifetime=5000}

or conversely remove the dynamic part of your page and put it in another template and include it as

{include file='head.html' nocache}

3.How to identify uniquely? Some pages have the same "name" but different content for specific members... think "myProfile.php"

for same page with different content like a profile page, you can pass profile Id as a parameter to cache call.

$my_cache_id = $_GET['profile_id'];    
$smarty->display('index.tpl', $my_cache_id);

This will ensure that same page with different parameters are not treated as same page.

Hope this helps.

在3种方式你可以通过这个名称保存缓存文件:myprofile_id例如注册的persone和他在用户表中的id为455你可以为他保存缓存文件myprofile_455之后你可以在tpl文件中包含缓存文件,就像这样:

{include file="cache/myprofile`$smarty.get.userid`.html"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM