Page 1 of 1

Session variables to cache HTML

Posted: Sat Nov 24, 2007 11:08 am
by Echilon
I have a script which uses a large amount of IF statements to check the contents of variables and generate HTML. These variables are loaded from a database, and don't change during the session. I'm wondering whether it would be considered bad practice to generate the HTML at the start of the session, then cache say 20Kb or so in a session variable.

It's actually for a dynamic navigation bar. The bar would only need to be generated once, then just echoed from the saved SESSION variable. Would this cause PHP to take a performance hit at all?

Posted: Sat Nov 24, 2007 2:21 pm
by Kieran Huggins
Sounds reasonable enough to me, though using templates is the typical solution to this kind of issue. have you considered either writing either a dead-simple template engine* or using one of the pre-built ones out there? Smarty is popular, though I've never used it personally.

*a session array & str_replace() could be considered a template engine, it doesn't have to be brain surgery.

Re: Session variables to cache HTML

Posted: Sun Nov 25, 2007 7:43 pm
by superdezign
Echilon wrote:Would this cause PHP to take a performance hit at all?
PHP has to load the session data anyway. Go for it. :P