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

save additional data in cache

 
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 -> General
View previous topic :: View next topic  
Author Message
nikotto
Smarty Rookie


Joined: 12 Jan 2004
Posts: 26

PostPosted: Wed May 10, 2006 2:49 pm    Post subject: save additional data in cache Reply with quote

I need to store some data in the cache - beside the html in the cache.

It works like this:
Code:
$smarty = new Smarty();
$smarty->caching = true;
if(!$smarty->is_cached('test.tpl')) {
  //store some data in the cache (expensive to calculate)
  $smarty->_cache_info['foo'] = 'bar';
} else {
   //read this data from the cache
   echo $smarty->_cache_info['foo'];
}
$smarty->display('test.tpl');


But this works only if i change one line in Smarty.class.php:
Smarty.class.php Line 1165
Code:
// save old cache_info, initialize cache_info
array_push($_cache_info, $this->_cache_info);
$this->_cache_info = array();


It looks like there is some kind of stack in the fetch-function that preserves the _cache_info variable - and i'm sure there is a reason for that.
Why is this needed? Would there be another way how i could save additional data in the cache?

thanks,
niko
Back to top
View user's profile Send private message Send e-mail
mohrt
Administrator


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

PostPosted: Wed May 10, 2006 4:37 pm    Post subject: Reply with quote

Smarty cache is designed to cache output of $smarty->display(). Assign your calculated values to a template variable, then display them in the template. The output will get cached.
Back to top
View user's profile Send private message Visit poster's website
nikotto
Smarty Rookie


Joined: 12 Jan 2004
Posts: 26

PostPosted: Wed May 10, 2006 4:51 pm    Post subject: Reply with quote

thanks for your reply.

the problem in my case is that i have uncached parts on my page (a formular that should output error-messages on submit)

And i actually don't know which formular is displayed. To avoid reading in that from the database (from the url) i save the information about the formular in the cache.

This saves me a few SQL-Queryies - so imho it is a good thing.

or is there any other way around it?

niko
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


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

PostPosted: Wed May 10, 2006 5:31 pm    Post subject: Reply with quote

Smarty wants its cache to be private so it is probably not a good idea to store application data in the Smarty cache. Just use your own cache code and establish a new directory for it. Something like Pear's cache_lite will work in a pinch, but there are many cacher's out there (I tend to use my own private code but you can find many cachers of good quality if you look around).

Sometimes I use special filters run on templates that represent data -- the idea being that the output of the template is either executable code or serialized representations of data. Using special "categories" of templates in that manner may help you solve your problem if you are bent on using Smarty for that. I still think it is easier/better to decouple your application's data caching needs from Smarty's output caching concerns.
Back to top
View user's profile Send private message
nikotto
Smarty Rookie


Joined: 12 Jan 2004
Posts: 26

PostPosted: Thu May 11, 2006 7:29 am    Post subject: Reply with quote

thanks again,

i do use cache_lite now - and it works really great - just as easy to implement as abusing smartys cache_info.

thanks for that tipp!
Back to top
View user's profile Send private message Send e-mail
i-litovan
Smarty Rookie


Joined: 03 Aug 2013
Posts: 13

PostPosted: Thu Oct 31, 2013 12:38 pm    Post subject: Re: save additional data in cache Reply with quote

nikotto wrote:
I need to store some data in the cache - beside the html in the cache.

It works like this:
Code:
$smarty = new Smarty();
$smarty->caching = true;
if(!$smarty->is_cached('test.tpl')) {
  //store some data in the cache (expensive to calculate)
  $smarty->_cache_info['foo'] = 'bar';
} else {
   //read this data from the cache
   echo $smarty->_cache_info['foo'];
}
$smarty->display('test.tpl');


But this works only if i change one line in Smarty.class.php:
Smarty.class.php Line 1165
Code:
// save old cache_info, initialize cache_info
array_push($_cache_info, $this->_cache_info);
$this->_cache_info = array();


It looks like there is some kind of stack in the fetch-function that preserves the _cache_info variable - and i'm sure there is a reason for that.
Why is this needed? Would there be another way how i could save additional data in the cache?

thanks,
niko



I also want to keep a small data into the template Very Happy
This can be done now?
Back to top
View user's profile Send private message
i-litovan
Smarty Rookie


Joined: 03 Aug 2013
Posts: 13

PostPosted: Thu Oct 31, 2013 12:48 pm    Post subject: Reply with quote

Maybe this will help ...
http://www.smarty.net/forums/viewtopic.php?t=8964
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 -> General All times are GMT
Page 1 of 1

 
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