开发者

Smarty Caching (With Dynamic Content)

开发者 https://www.devze.com 2023-02-13 18:54 出处:网络
I have a very dynamic (social networking) site running smarty that I want to enable caching for. My Structure:

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.t开发者_C百科pl

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


in 3 way u can save cache file by this name: myprofile_id for example a persone that registered and his id is 455 in user table u can save cache file for he with this name myprofile_455 after that u can include cached file in tpl file like this:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号