View previous topic :: View next topic |
Author |
Message |
nikotto Smarty Rookie
Joined: 12 Jan 2004 Posts: 26
|
Posted: Wed May 10, 2006 2:49 pm Post subject: save additional data in cache |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed May 10, 2006 4:37 pm Post subject: |
|
|
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 |
|
nikotto Smarty Rookie
Joined: 12 Jan 2004 Posts: 26
|
Posted: Wed May 10, 2006 4:51 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 10, 2006 5:31 pm Post subject: |
|
|
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 |
|
nikotto Smarty Rookie
Joined: 12 Jan 2004 Posts: 26
|
Posted: Thu May 11, 2006 7:29 am Post subject: |
|
|
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 |
|
i-litovan Smarty Rookie
Joined: 03 Aug 2013 Posts: 13
|
Posted: Thu Oct 31, 2013 12:38 pm Post subject: Re: save additional data in cache |
|
|
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
This can be done now? |
|
Back to top |
|
i-litovan Smarty Rookie
Joined: 03 Aug 2013 Posts: 13
|
|
Back to top |
|
|