Smarty caching (with dynamic content)

I have a very dynamic (social network) site running smarty, for which I want to enable caching.

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 scripts, banner, footer, etc. How can I cache these, but not specific parts, that look different depending on whose input (among other factors)

I discovered 3 methods:

  • {nocache} {include='indexContent.tpl'} {nocache}
  • {dynamic} {include ...
  • Set cache_id for each page.

Unfortunately, each of them has a problem:

  • Really not working? Dynamic content is still cached.
  • Not sure how to implement or how it differs from (1)
  • ? "", ... , "myProfile.php"

? !!

+3
4

. , (, ), -

NEWS_BLOCK

script, preg_replace .

$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);
+1

-, , Varnish, Server-Side Includes ( Varnishi ESI). URL-, , .

, HTML- :

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

/esi/indexContent.php script, .

: gzipped deflated ESI,

+4

3- u : myprofile_id, , , , - 455 , myprofile_455 u tpl :

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

, , , - .

, , . ,

  • ? - .
  • , (1)

, , tpl. tpl

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

or vice versa, delete the dynamic part of your page and put it in another template and include it as

{include file='head.html' nocache}

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

for the same page with different content, such as a profile page, you can pass the profile identifier as a parameter to cache the call.

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

This ensures that the same page with different parameters will not be considered the same page.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1795306/


All Articles