Session variables to cache HTML

Discussion in 'PHP' started by Echilon, Nov 24, 2007.

  1. #1
    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?
     
    Echilon, Nov 24, 2007 IP
  2. pluswebdev

    pluswebdev Banned

    Messages:
    205
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    are these menus are user dependence (different menus for different users)? if not you can just use to write the menu structure to plain txt file and regenerate it when you make some changes on your menu structure
     
    pluswebdev, Nov 24, 2007 IP
  3. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah well said pluswebdev.

    The problem with that approach Echilon is that the menu needs to be generated for each user. When in reality its probably better to generate it only once for all users.

    I dont particularly like storing information like this as a session variable. Try storing it to disk or better yet chuck it into memcached if you are able to install it on the server.
     
    tonybogs, Nov 24, 2007 IP
  4. Echilon

    Echilon Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Each user will see a different menu. I could cache it to a file for each user, but that would probably generate more overhead than regenerating it on every page load.
     
    Echilon, Nov 24, 2007 IP
  5. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What aspects of the menu will be different?

    If its something like a user name you can store the menu once on disk, and just the name in a session variable.

    That way when you cache the menu you can place the text #name# inside and replace that with the session variable everytime you load the page.

    I highly recommend memcached though, zero CPU load and minimal overhead.

    Good Luck :)
     
    tonybogs, Nov 25, 2007 IP