Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

{nocache} Patch for 2.5.0 CVS
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Wed May 21, 2003 12:29 pm    Post subject: {nocache} Patch for 2.5.0 CVS Reply with quote

    ADMIN NOTE: (2003-Jun-21): The CVS now includes an alternate to Andre's implementation, though it borrows/uses many of the same ideas that Andre presents here.


Puhh.... I have finally ported my {nocache} patch to Smarty 2.5.0 CVS Very Happy
The following ZIP file includes both the .diff files and already patched versions of the Smarty and Smarty_Compiler classes. Additionally the needed core plugins are included.

Download: http://cmsng.sourceforge.net/Smarty/Smarty_nocache.zip

It was even easier to implement thanks to the great new structure of the Smarty Classes Wink

Source code changes:
  • Smarty.class.php
  • Smarty_Compiler.class.php
  • plugins/core.read_cache_file.php
  • plugins/core.write_cache_file.php
  • plugins/core.run_insert_handler.php
  • plugins/core.exists_cache_file.php NEW!!!
I added the core function exists_cache_file( ) which checks if a cached file exists and is up-to-date. This function is now used in Smarty::is_cached( ) instead of read_cache_file( ). The functions are not fully optimized yet but they work great and they are already very fast.

Differences to the old {nocache} patch:
  • In the old version your cached template got compiled and evaluated each time it was called.
    Now I store pure PHP code into your cache files and include() them which is really fast Shocked
  • Custom cache handlers will still work. But instead of including the cache file it gets evaluated which is a bit slower.
  • Most cache logic was moved into the new "core"-plugins of Smarty which makes them easily replaceable.


Usage:
First extract all files into your /Smarty/libs directory overwriting the old files.

[php:1:158a2c3383] // Custom Functions

function smarty_block_nocache($params, $content, &$smarty) {
return $content;
}

function smarty_function_currenttime($params, &$smarty) {
return date("Y-m-d H:i:s");
}
[/php:1:158a2c3383][php:1:158a2c3383] // Main Application

$smarty = new Smarty();
$smarty->register_block("nocache", "smarty_block_nocache", false);
$smarty->register_function("currenttime", "smarty_function_currenttime", false);

$smarty->assign("time", date("Y-m-d H:i:s"));

$smarty->caching = 1;
$smarty->display("myTemplate.tpl");

[/php:1:158a2c3383]
Code:
   {* My Template *}
 
   Example for caching ...
   Current Date & Time (cached): {$time}
   Current Date & Time (not cached): {currenttime}
   Current Date & Time (not cached): {nocache}{$time}{/nocache}


Remarks:
  • Not everything is fully tested yet but it works quite well in my developing environment
  • Not everything is fully optimized. Expect more speed in the near future.
  • As far as I can see there are no conflicts with other Smarty functions.
  • Delete all (!!) compiled templates (e.g. /templates_c) and all (!!) cached templates (e.g. /cache)
  • This is a patch made for developers and power-users. I do not recommend it for Smarty newbies Wink
  • Feel free to test it and report bugs.

Download: http://cmsng.sourceforge.net/Smarty/Smarty_nocache.zip

Have fun,
André
Back to top
View user's profile Send private message
Carsten
Smarty Rookie


Joined: 15 Jul 2003
Posts: 5
Location: Hamburg, Germany

PostPosted: Tue Jul 15, 2003 8:56 pm    Post subject: Question to the CVS Version... Reply with quote

I have a small quite strange Problem with the CVS Version:

First I want to mention, I was really happy, that you implemented something like making a modifier not cachable, it was just what i was searching for. Thanks in advance.

But I have a tiny problem:
I have some information-pages with some master detail information. For these pages I want to use the caching, although one detail should not be cached: A 'Days to' Information text. I wrote a function for that, which calculates the information out of a static, cached timestamp. This part should of course not be cached.
For some pages this works really fine, but on my main Information page i get the following Error-Message, when using the cached version:

Quote:

Fatal error: Call to undefined function: _smarty_tplfunc_2f0adb00fa69c124bdd365b148a272aa_0() in C:\Programme\Apache Group\Apache2\htdocs\Nutzfahrzeuge\php\smarty\Smarty.class.php on line 1792


The really strange thing: This only happens on the main Information tab.
I set the cache Lifetime to -1, for keeping the cache until i delete them manually.

I use the following wrapper Class for smarty, where i also register the function:
[php:1:1a97aab529]
<?PHP
require_once(BASEDIR.'smarty/Smarty.class.php');
require_once(BASEDIR.'tools.inc.php');

//! Wrapper-Klasse für Smarty, setzt Verzeichnisse und Caching Funktionen:
class page extends smarty {
//! Diese Funktion erzeugt ein neues Seiten Objekt und setzt die Caching Funktionen von Smarty
function page($cache = true, $cache_lifetime = 300) {
// Smarty Constructor aufrufen:
$this->Smarty();

$this->register_function('date_to', 'dateDiff', false);

// Verzeichnisse setzen:
$this->template_dir = 'template/';
$this->compile_dir = 'template/compiled/';
$this->config_dir = 'template/config/';
$this->cache_dir = 'template/cache/';

// Debug-Flag setzen:
$this->debugging = DEBUGGING;

// Caching-Funktionen setzen:
$this->caching = $cache;
$this->cache_lifetime = $cache_lifetime;
}
}
?>
[/php:1:1a97aab529]

Big thanks in advance,

Carsten
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Jul 15, 2003 9:44 pm    Post subject: Reply with quote

FYI: the implementation of "nocache" in cvs is not the one André posted. i think they share a common approach, but handle the the php-code to be executed when displaying a cached tempate quite differently.

can you post (or mail me) some template-code that triggers the above error? (the shorter the better of course Smile )

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Carsten
Smarty Rookie


Joined: 15 Jul 2003
Posts: 5
Location: Hamburg, Germany

PostPosted: Wed Jul 16, 2003 7:15 pm    Post subject: Reply with quote

Here you go:
It is not yet optimized yet, please do not wonder:
(I reduced it a little bit, if something is missing, please tell me)
[php:1:0878e336ba]
<?php
session_start();

// Eine Seite, die einen Login benötigt
if ((!isset($_SESSION['benutzer'])) || (empty($_SESSION['benutzer']))) {
Header('Location: index.php');
die('Nicht eingeloggt.');
} else {
define('BASEDIR', 'php/');
require_once(BASEDIR.'page.inc.php');

$seite = new page(true, -1);

if (isset($_GET['pkw'])) {
require_once(BASEDIR.'pkw.inc.php');
$fahrzeug = new pkw((int)$_GET['id']);
$cache_prefix = 'pkw';
$default_tpl = 'detailpkw.tpl';
$seite->assign('type', 'pkw');
$seite->assign('title', 'PKW: '.$fahrzeug->hersteller.' '.$fahrzeug->modell);
} else {
/////////////////////////////
// other kinds of cars here, same as above.
/////////////////////////////
}
if (isset($_POST['frage'])) {
$fahrzeug->frage($_SESSION['benutzer']['FIRMA'], $_SESSION['benutzer']['ID'], $_POST['frage']);
$seite->clear_cache('detail.tpl', $cache_prefix.$fahrzeug->id.'faq');
}
/////////////////////////////
// other actions follow here, like the above sample
/////////////////////////////

$cache_id = $cache_prefix.(int)$_GET['id'].$_GET['seite'];

if ($_GET['seite'] == 'weiter') $seite->assign('content', 'detail2.tpl');
/////////////////////////////
// other subpages follow here, like the above sample
/////////////////////////////
} else $seite->assign('content', $default_tpl);

$seite->assign('fahrzeug', $fahrzeug);
// This File contains the menu structure.
include(BASEDIR.'menu/details.inc.php');
// This file contains code for handling menu-actions
include(BASEDIR.'menu.inc.php');
$seite->display('detail.tpl', $cache_id);
?>
[/php:1:0878e336ba]
Nothing spectacular. That is the reason, why i do not understand the error.


Here the timeDiff Function (to be found in the tools.inc.php), registered for use with smarty in the page class.
[php:1:0878e336ba]
function dateDiff($s) {
if (is_array($s)) $s = $s['timestamp'] - time();

$m = intval($s/60);
$s = $s % 60;

$h = intval($m/60);
$m = $m % 60;

$d = intval($h/24);
$h = $h % 24;

$diff = "";
if ( !$d == "0" ) $diff = $diff . "$d Tage ";
if ( !$h == "0" ) $diff = $diff . "$h Stunden ";
if ( !$m == "0" ) $diff = $diff . "$m Minuten ";
if ( !$s == "0" ) $diff = $diff . "$s Sekunden ";
if ( $diff == "" ) $diff = "-";
trim ($diff);

return $diff;
}
[/php:1:0878e336ba]

Here the main-Template:
Code:

{include file="headerneu.tpl"}
{include file=$content}
{include file="footer.tpl"}


And here the content-templates:
Non working detailpkw.tpl:
Code:

                 <TABLE WIDTH="100%">
                  <TR>
{* Left out some <tr></tr> *}
                                  <TR>
                                         <TD><B><FONT FACE="Arial">Endet am:</FONT></B></TD>
                                         <TD><FONT FACE="Arial">{$fahrzeug->enddatum_text}<br>(in {date_to timestamp=$fahrzeug->enddatum})</font>
                                         </TD>
                                  </TR>
{* Left out some <tr></tr> *}
                </TABLE>


Working detail2.tpl:
Code:

                 <TABLE WIDTH="100%">
{* Left out some <tr></tr> *}
                                  <TR>
                                         <TD><B><FONT FACE="Arial">Endet am:</FONT></B></TD>
                                         <TD><FONT FACE="Arial">{$fahrzeug->enddatum_text}<br>(in {date_to timestamp=$fahrzeug->enddatum})</font>
                                         </TD>
                                  </TR>
{* Left out some <tr></tr> *}
                </TABLE>


Looks pretty the same, doesn't it?
$fahrzeug->enddatum is a unix-timestamp.
Hope it helps.
If you need the complete code, please send me a message and i will send it to you via email.

Thanks,
Carsten

P. S.: I know that the CVS-Version is a different Implementation, so maybe the message was off-topic here.
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jul 16, 2003 11:04 pm    Post subject: Reply with quote

i see the problem is the "dynamic" include: {include file=$content}

if $content is different at display-time of a cached template than it was at cache-generation-time, the above error occurs.

i don't know how to handle it, yet. but i'll tell you when it's fixed Smile

many thanks so far.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Carsten
Smarty Rookie


Joined: 15 Jul 2003
Posts: 5
Location: Hamburg, Germany

PostPosted: Thu Jul 17, 2003 7:40 am    Post subject: Thanks! Reply with quote

Thanks for your help and good luck fixing the problem.

Since in my project there are only two pages affected, i will paste the header and the footer file into the template, a small work-around for my problem (for the presentation).

By the way, another question: Does the implementation also include a {nocache}section like the one, suggested by Andre?

Keep up the great work! Very Happy

Carsten
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Thu Jul 17, 2003 11:45 am    Post subject: Reply with quote

messju wrote:
FYI: the implementation of "nocache" in cvs is not the one André posted. i think they share a common approach, but handle the the php-code to be executed when displaying a cached tempate quite differently.


The nocache-solution of CVS version of Smarty works great for me. I recently removed all my changes and used the offical CVS. No problems at all! Great work guys Wink

Carsten wrote:
By the way, another question: Does the implementation also include a {nocache}section like the one, suggested by Andre?


The following code which worked with my patch should now also work with the official CVS version:
[php:1:a215991ed1] // Custom Functions

function smarty_block_nocache($params, $content, &$smarty) {
return $content;
}

function smarty_function_currenttime($params, &$smarty) {
return date("Y-m-d H:i:s");
}

// Main Application

$smarty = new Smarty();
$smarty->register_block("nocache", "smarty_block_nocache", false);
$smarty->register_function("currenttime", "smarty_function_currenttime", false);

$smarty->assign("time", date("Y-m-d H:i:s"));

$smarty->caching = 1;
$smarty->display("myTemplate.tpl");[/php:1:a215991ed1]

For your information: My patch is NOT compatible with the newest CVS and it won't be updated anymore. As far as I can see the CVS is great and works exactly the same way even if the storage of the cached functions is handles a bit different. Also I recently detected some bugs in my patched code which made {insert} function behave incorrectly some times.
Thanks again for this great CVS version Very Happy

Bye,
André
Back to top
View user's profile Send private message
Carsten
Smarty Rookie


Joined: 15 Jul 2003
Posts: 5
Location: Hamburg, Germany

PostPosted: Thu Jul 17, 2003 12:39 pm    Post subject: Reply with quote

Hi,

I thought about excluding something like your smarty_block_nocache from caching... You're right, that might help me; i haven't thought about it that way...

Wanted to do something like:
Code:

{nocache}
{if $smarty.now < $fahrzeug->enddatum}
{* some content here *}
{/if}
{/nocache}


But of course, i could simply use a block... Thats simple! Why didn't i think about it myself?!?

Thanks,

Carsten
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Jul 17, 2003 7:38 pm    Post subject: Reply with quote

@carsten:

okay, can you please cvs upd/clear_all_cache/clear_compiled_tpl and recheck your fahrzeug-enddatum-nocache-stuff Smile.
i am not 100% sure if it is fixed for all cases (the display/cache-queue is a bit complicated) but i am optimistic.

of course everybody else is also encouraged to test the nocache-paramter and it's implications and to test the cvs-version overall. it always better to have people who say the "current cvs has bugs here and there" than the ones who say "your latest release sucked" Wink

@andré: i'm glad to hear you get along with current cvs, i was a bit unsure since i know you put quite some effort in your implementation but i took a slightly different road.

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Carsten
Smarty Rookie


Joined: 15 Jul 2003
Posts: 5
Location: Hamburg, Germany

PostPosted: Thu Jul 17, 2003 10:06 pm    Post subject: Reply with quote

Seem's to work so far. Very Happy
Unfortunately with one tiny detail not working (i am not sure, if it is meant that way) Sad
The parameter assigned to that function / block seems not to be cached.
And so unfortunately my template does not work:
{date_to timestamp=$fahrzeug->enddatum}
The function gets a parameter called 'timestamp', but it is empty.
In my previous version $fahrzeug was assigned to the template, though it was not used, i thought. Now i stopped assigning it and stopped reading it from the db, everything is fine exept the date_to information.

Anyway, thanks so far.
Carsten

Addendum (July, 18th 2003)
I replaced the function with a modifier (make more sense to me): Same result, as i expected.
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Jul 21, 2003 10:32 am    Post subject: Reply with quote

messju wrote:
@andré: i'm glad to hear you get along with current cvs, i was a bit unsure since i know you put quite some effort in your implementation but i took a slightly different road.


Yeah, sure I put quite some effort in my solution. But this was because I NEEDED a solution and couldn't wait Wink
And it helped me alot to understand the inner functionality of Smarty. So it wasn't wasted time Wink
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Jul 21, 2003 10:40 am    Post subject: Reply with quote

Carsten wrote:
The parameter assigned to that function / block seems not to be cached.
And so unfortunately my template does not work:
{date_to timestamp=$fahrzeug->enddatum}
The function gets a parameter called 'timestamp', but it is empty.


This is exactly the same problem I had with my patch. I haven't tested CVS for this yet but I think it could easily be possible that this bug (?) exists there too.

I'm really not sure if a parameter should be cached or not. Think of the following:

1) {insert type="foo" value=$bar}
$bar should be cached because it was always cached in older versions of Smarty (if I remember correctly)

2) {foo_not_cached param=$bar}
Should $bar be cached or not?!

3) {foo_not_cached_block}{$bar}{/foo_not_cached_block}
$bar shouldn't be cached, right ?!

3) {foo_not_cached_block}{foo_normally_cached param=$bar}{/foo_not_cached_block}
What to do here?!
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jul 21, 2003 10:54 am    Post subject: Reply with quote

the parameters are not cached. i left this out intentionally for now.

{insert} caches it's params. this is good for some cases (for carsten's case above for example).

but it is bad for other case: if you use $smarty.request.foo or so as a parameter to an insert-function, one would not expect to get a "static" value (namely the cached one) to be passed to the insert function.

of course it was far easier to leave the caching of params out completely. i don't feel very sure, if this is good, but i know of a lot of problematic cases if we tried to cache them Sad
Back to top
View user's profile Send private message Send e-mail Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Aug 06, 2003 12:09 pm    Post subject: Reply with quote

FYI: i just committed my latest changes to the cache-handling. they regard parameter-caching.

by default no parameters are cached (as opposite to {insert} where all parameters are cached). but you can specify a list of parameters of the function that should be cached.

in Carstens example this would be:
[php:1:0f65a7dd8b]$this->register_function('date_to', 'dateDiff', false, array('timestamp'));[/php:1:0f65a7dd8b]

everybody is encouraged to test this and report to me if it doesn't behave Smile.
i hope the nocache-api to be final and if nothing dramatic crops up, this will go as is to the upcoming release candidate version of smarty.

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Aug 06, 2003 12:15 pm    Post subject: Reply with quote

andre wrote:
1) {insert type="foo" value=$bar}
$bar should be cached because it was always cached in older versions of Smarty (if I remember correctly)

yep.

Quote:
2) {foo_not_cached param=$bar}
Should $bar be cached or not?!

it is not cached by default.

Quote:
3) {foo_not_cached_block}{$bar}{/foo_not_cached_block}
$bar shouldn't be cached, right ?!

right.

Quote:
3) {foo_not_cached_block}{foo_normally_cached param=$bar}{/foo_not_cached_block}
What to do here?!

the param won't be cached. everything inside the outermost not-cached block works like $smarty->caching was false. hence the function is called on everytime it is displayed.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP