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
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
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Apr 29, 2003 12:54 pm    Post subject: Reply with quote

Quote:
I have modified the _compile_tag() function of Smarty_Compiler to recognize if a tag is cached or not.

[snip]

Registering "countComments" as not-cached function is fully transparent and you even don't need to change the existing templates. For the designer it doesn't matter what portions of the templates are cached and which are not.


Cool. I see what you are getting at and I agree that your technique of registering functions as caching or not is superior to the current state of insert Wink.

You make a good point about keeping caching issues out of the template design. I have to agree that this seems like the sane approach.

Heres a Q:

How would I go about implementing a page that was cached but had a block that was not cached. Here's the caveat: the uncached block is actually to be determined by the user priority. Level 1 gets a cached block with a 1 hour refresh; Level 2 gets a cached block with a 30 minute refresh; Level 3 gets a cached block with a 15 minute refresh; Level 4 gets an uncached block.

I guess I could set up a switch in my PHP code and setup smarty as appropriate. Or use a compile_id?? What if I wanted to do this in template Very Happy ?

What would you suggest?
Back to top
View user's profile Send private message
andre
Smarty Pro


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

PostPosted: Tue Apr 29, 2003 1:34 pm    Post subject: Reply with quote

boots wrote:
How would I go about implementing a page that was cached but had a block that was not cached. Here's the caveat: the uncached block is actually to be determined by the user priority. Level 1 gets a cached block with a 1 hour refresh; Level 2 gets a cached block with a 30 minute refresh; Level 3 gets a cached block with a 15 minute refresh; Level 4 gets an uncached block.


Same here in my project. I'm going another way. Here's some pseudo code:

[php:1:624d3d0ac7]
/**
* Display a block
*/
function smarty_function_block($block_id, &$smarty) {
$newSmarty = $smarty; // create a copy of smarty object
$newSmarty->caching = 1;
if ($level == 1)
$newSmarty->cache_lifetime = 60*60;
else if ($level == 2)
$newSmarty->cache_lifetime = 60*30;
else if ($level == 3)
$newSmarty->cache_lifetime = 60*15;
else
$newSmarty->caching = 0;
$newSmarty->assign("content", getBlockContent($block_id));
return $newSmarty->fetch(getBlockTemplate($block_id));
}
[/php:1:624d3d0ac7]

[php:1:624d3d0ac7]
/**
* Main Application
*/
$smarty = new Smarty;

// Blocks implement their own caching routine so we set
// caching flag to FALSE
$smarty->register_function ("block", "smarty_function_block", false);

// ... assign some values
$smarty->assign("foo", "bar");

// enable caching for master template
$smarty->caching = 1;
$smarty->cache_lifetime = 60 * 60 * 24; // one day lifetime
$smarty->display("myTemplate.tpl");
[/php:1:624d3d0ac7]

Code:

  {* My Template *}
  ... some content ...
  {block id="whoIsOnline"}
  ... some content ...
  {block id="newestHeadlines"}
  ... some content ...


Hope you understood what I meant?! This is the way I'm using Smarty. Not 100% clean code (copying objects is ugly in my eyes) but it works very nice Wink
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Apr 29, 2003 2:22 pm    Post subject: Reply with quote

Thanks andre.

One thing I don't get: I thought that once you set a cache lifetime for a given object, you couldn't change it later unless you used a different cache_id. Did you just leave that detail out or is that something else your patch does?
Back to top
View user's profile Send private message
andre
Smarty Pro


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

PostPosted: Tue Apr 29, 2003 2:25 pm    Post subject: Reply with quote

boots wrote:
One thing I don't get: I thought that once you set a cache lifetime for a given object, you couldn't change it later unless you used a different cache_id. Did you just leave that detail out or is that something else your patch does?


Oops Embarassed You're right. So set the cache id to the block's ID + $level or something like this.

[php:1:1235a0bf27]
// ... [snip]
$newSmarty->assign("content", getBlockContent($block_id));
$cache_id = $block_id."|".$level;
return $newSmarty->fetch(getBlockTemplate($block_id), $cache_id);
// ... [snip]
[/php:1:1235a0bf27]
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Apr 29, 2003 2:37 pm    Post subject: Reply with quote

Quote:

Cool. I see what you are getting at and I agree that your technique of registering functions as caching or not is superior to the current state of insert Wink.


Smarty will be getting some caching facelifts soon, that is the next big project. We have a pretty good idea how it is going to work, and we have Andre's code to compare against. It's all good stuff to have. Smile

The caching will definately be by registeration, keeping things out of the templates as much as possible. You can think of {insert} as just a custom function registered as non-cached.

Monte
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


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

PostPosted: Tue Apr 29, 2003 2:59 pm    Post subject: Reply with quote

Hi Monte,

I have already mailed with Messju and he explained a bit of your ideas to me (in german language Wink )

Sounds very interessting!


An idea I just had and I'm not sure if it would be practical but what if I just replace the not-cached code within a cache file with pure PHP tags so the cached file gets just included if displayed?!

The way I'm doing it now: Cached template:
Code:

   Example for caching ...
   Current Date & Time (cached): 12:49:15
   Current Date & Time (not cached): <!--{#SMARTYCACHE#currenttime}-->
   Current Date & Time (not cached): <!--{#SMARTYCACHE#nocache}--><!--{#SMARTYCACHE#$time}--><!--{#SMARTYCACHE#/nocache}-->   


The way I could do it:
Code:

   Example for caching ...
   Current Date & Time (cached): 12:49:15
   Current Date & Time (not cached): <?php $this->_plugins["functions"]... ?>
   Current Date & Time (not cached): <?php ... ?>


Then I wouldn't need another file. Would this break something? I have to think about it ... Rolling Eyes
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Apr 29, 2003 4:08 pm    Post subject: Reply with quote

Monte: can't wait to see how it develops.

Andre:
Quote:
An idea I just had and I'm not sure if it would be practical but what if I just replace the not-cached code within a cache file with pure PHP tags so the cached file gets just included if displayed?!


I was going to suggest something like this. This is similar to what I did to get templated config files. Wouldn't you just need to save the compiled file to the cache? Then you use force_compile to get a fresh cache. Its hacky though.

Quote:
Then I wouldn't need another file. Would this break something? I have to think about it ... Rolling Eyes


I think you would have to ensure that your non-cached code portions that ended up living in cache files had cache_id's different than the calling template. Either that or a different compile_id?

I thought it would be interesting if the last parameter in your register_function (true/false) also allowed an optional associative array of cache_name->ttl's. Right now, if you pass true, you get page level caching of the tag. If you pass false, you get insert-type functionality or as per your example, a form of tag level caching of the object. If an array is passed, you'd get the latter in a slightly automated form with the array being used to set up named caches with different ttls.

Just a thought.

I'm installing the patch right now! Finally!
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Tue Apr 29, 2003 4:55 pm    Post subject: Reply with quote

monte: PLEASE give us something like {nocache}-blocks, too. It's often that I couldn't do something with a non-cached function, because it's not always exactly the same thing I want to put out or whatever... see my posting above on this for a little example... always throwing the non-cached parts into extra templates and then let these templates be returned by non-cached functions doesn't make sense, I may well use inserts for that, too
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Apr 29, 2003 5:55 pm    Post subject: Reply with quote

One thing that I like about this patch is that it allows non-cached block functions which is not at possible in the current Smarty, even with insert-type syntax.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Apr 29, 2003 6:51 pm    Post subject: Reply with quote

A {nocache}{/nocache} block would simply be a block function registered as non-cached.

Monte
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Apr 29, 2003 7:02 pm    Post subject: Reply with quote

mohrt wrote:
A {nocache}{/nocache} block would simply be a block function registered as non-cached.


Exactly! That's what I like! (because now any PHP function can now be used in a non-cached block).

Off-topic: Is there a performance hit when using block functions compared to non block functions?
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Tue Apr 29, 2003 7:38 pm    Post subject: Reply with quote

D'Oh, forgive me, Monte, I should use my own brain more often Wink
Back to top
View user's profile Send private message
andre
Smarty Pro


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

PostPosted: Tue Apr 29, 2003 8:18 pm    Post subject: Reply with quote

boots wrote:
I think you would have to ensure that your non-cached code portions that ended up living in cache files had cache_id's different than the calling template. Either that or a different compile_id?


Think you misunderstood me ... I won't change the current cache behaviour. But now (without my patch) a {insert} function ist compiled and cached as {SOMETHINGUNIQUE...serialized_array...SOMETHINGUNIQUE}

At runtime this is replaced via pregex with the result of a function call taking the serialized array as parameter.

So why don't we change {SOMETHINGUNIQUE...} to <?php ... ?> and call the function directly?! So we would just need to include the cached template to get it executed. This is what I wanted to do. Instead of <!--{#SMARTYCACHE# ... }--> write the PHP code directly in the appropriate cache file.

... sorry... need to go NOW... I'm posting this entry unread (!!!) ... so don't blame me Wink Bye
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Tue Apr 29, 2003 9:15 pm    Post subject: Reply with quote

I guess this is the best idea - just write the PHP code to the cache. {insert}s are sloooooooooooooooow Smile
Back to top
View user's profile Send private message
andre
Smarty Pro


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

PostPosted: Tue Apr 29, 2003 9:19 pm    Post subject: Reply with quote

Soo... back again Smile

The only problem I see with the above two posting is the following:

Cache files can be stored everywhere. It depends on your cache file handler. So including with include() function is not always possible. So this raises another question:

1) Templates can be loaded from anywhere (because they don't need to be included)
2) Compiled templates needs to resist in the file system (because they get included)
3) Cached templates can resist anywhere again

I think a upcoming Smarty version will also allow compiled templates to be stored anywhere. So question to Monte / Messju: Is this planned? If yes, how do you think you can implement this? So how do you "include" the compiled template if its located in a DB for example?
Exactly this (yet unknown) way I would prefer for including the cached templates with PHP in them. I know the idea Messju (Monte?) had about separating the executable code from the cached template and store it in a separate file. This way the separated code can be easily included again which gives performance. BUT WHAT if you don't want Smarty to create files in your filesystem? If you want it to store EVERYTHING within your database?

So perhaps we could create a Smarty method here to execute the script code located in a string (e.g. a file on your hard drive or a string stored within a DB). This method should be so flexible it can execute a code on your harddrive by including it (use "include()" function) or to eval() it if it comes from another source. This way the execution / inclusion of a source (say template, compiled template or cached template) is fully transparent to the Smarty class.

Back to the point of discussion Wink :
If you store your cached templates onto your file system it will be included by "include()" which is fast and is even more accelerated by PHPAccelerator. If your cached templates lie outside of your file system into a database for example the output gets evaled with "eval()" which is quite slow compared to the first method.

Hope you understand what I wanted to say. Please ignore the typos because I'm drunk at the moment Embarassed but most people say I am most productive if I am drunk Wink (it's 11 p.m here Wink )
Back to top
View user's profile Send private message
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