SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: caching

  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    caching

    i have a template system i made and it writes everything to a variable. i want to cache, and i was thinking about just writing this variable to both a file and the browser, so i can have it cached for x seconds. the only problem is my pages are probably 25kb each, and i have about 100k possible different pages.

    i haven't done the math, but i think this will ad up real quick, and i only hav 400mb of disk space. so, i think the conventional method of caching the entire method would take up too much space.

    i was thinking that instead of caching the entire file, i could just cache the variables replaced in the template file (all of the {TITLE}, {IMAGE}, etc.).

    i have not seen this done before, so i was wondering what benefits or downfalls there would be to taking this path.

  2. #2
    SitePoint Wizard Nikolas's Avatar
    Join Date
    Feb 2005
    Location
    Greece
    Posts
    1,222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe you should cache only content that is generated with complex algorithm because propably your site will be slower by caching variables.
    I believe that a query is faster than reading n files on each page load.
    If you will stack with this try another method of getting faster pages : eaccelarator it is great

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i was thinking that instead of caching the entire file, i could just cache the variables replaced in the template file (all of the {TITLE}, {IMAGE}, etc.).
    At the moment I generate a web page on the fly, using Composites. Due to the uniqueness of each Composite, I'm able to cache a single Composite (HTML fragment once generated), and one or more/all of a given Composites child Composites as well at run time.

    In this case, this can save a good bit of processing power once the page becomes complicated with a lot of dynamically generated fragments, but in your case if all you have is a few variables, then it may well be just as well to just cache the whole lot in one go?

    In my case, the generation and cacheing is extremely easy to implement and maintain (due to the nature of Composites) but your case is different and could be more complex and time consuming to handle the logic <> number of tags... Is the trade off going to be advantageous I'm pondering.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for you guys' replies

    Maybe you should cache only content that is generated with complex algorithm because propably your site will be slower by caching variables.
    I believe that a query is faster than reading n files on each page load.
    i was thinking of actually caching all the variables in one file...not each variable individually. would that still work? i was thinking of having a delimiter line to seperate each variable, like "<--- TITLE --->", or similar.

    At the moment I generate a web page on the fly, using Composites. Due to the uniqueness of each Composite, I'm able to cache a single Composite (HTML fragment once generated), and one or more/all of a given Composites child Composites as well at run time.
    im sry, but i dont really understand what composites are. are you saying that you are caching SEGMENTS of your code, then compiling it all together when it runs? that actually seems like that would work, because not only would it cache, but also you can set how long each segment caches seperately... like users online in a forum wouldn't cache more than 5 minutes, while older forums can be cached for days.

    but your case is different and could be more complex and time consuming to handle the logic <> number of tags...
    my app is extremely simple compared to many apps out there. there isn't really any complex logic, yet.

    If you will stack with this try another method of getting faster pages : eaccelarator it is great
    forgive my ignorance, but im not quite sure what stacks are, either. i would've used a pre-built app for setting things up, but my server is not extremely powerful, so i dont want the overhead of code that wont be used specifically for my purpose.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    would it be smarter to cache information in a database or a file

  6. #6
    SitePoint Addict SRTech's Avatar
    Join Date
    Mar 2005
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by XtrEM3
    i was thinking of actually caching all the variables in one file...not each variable individually. would that still work? i was thinking of having a delimiter line to seperate each variable, like "<--- TITLE --->", or similar.
    If I were you, I might try using serialize() / unserialize() for this, instead of a custom function.

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    File caches are usually faster to access, no doubt this is the general consensus.

    then compiling it all together when it runs? that actually seems like that would work, because not only would it cache, but also you can set how long each segment caches seperately... like users online in a forum wouldn't cache more than 5 minutes, while older forums can be cached for days.
    Exactly. Each Composite once cached is independent of other Composites, so you can cache each Composite (and it's child Composites) independently, pulling in a given Composite cache, or apply the (newly) generated HTML as is...

    Then, as you recurse over the Composite structure, your fragmented HTML is assembled. Because of the nature of Composites (and the separate cache process), it doesn't really matter where the HTML comes from, at the end of the day.

    For more on the Composite design pattern, you could have a look at an example and on other design patterns, at www.dofactory.com for one resource (highly recommended). Also, google for the Composite design pattern.

    Also, search this forum and my posts as I've made a few advances in more recent times using the Composite (with Visitor) pattern(s) which I've posted quite a bit of script to this forum for others to learn from.

    If you've any questions, I'll try to provide answers, but I can't release any more script (for the forseeable future at least ).

  8. #8
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by XtrEM3
    i have a template system i made and it writes everything to a variable. i want to cache, and i was thinking about just writing this variable to both a file and the browser, so i can have it cached for x seconds. the only problem is my pages are probably 25kb each, and i have about 100k possible different pages.

    i haven't done the math, but i think this will ad up real quick, and i only hav 400mb of disk space. so, i think the conventional method of caching the entire method would take up too much space.

    i was thinking that instead of caching the entire file, i could just cache the variables replaced in the template file (all of the {TITLE}, {IMAGE}, etc.).

    i have not seen this done before, so i was wondering what benefits or downfalls there would be to taking this path.
    Yes, what you can do is to put all variables in an array an cache the serialized string. That will still provide great speedup if your data comes from a database.

    Sometimes, I do that, not because I have your disk space constraints, but because the cached information is not meant to be display directly. For instance, I cache session user profiles to avoid retrieving them from the database in all accesses of logged users.

    I use this generic caching class that stores cache data in files and a fast and robust way, i.e. it uses exclusive locks for updating and shared locks for reading. This way, the cache files do not get corrupt and simultaneous read access to the cache files presents no delays.

    OTOH, if you are caching content of full pages, you may consider caching compressed versions of the pages. Using zlib it is easy to achieve compression ratios of 5:1.

    Since most browsers these days, already support compression, you could serve the compressed cached pages immediately without further overhead, making the Web page serving throughput even faster.

    For browsers that do not support compression, the cached pages would have to be decompressed, but that would not happen so often to make you worry about decompression overhead.

    The same class mentioned above could be used to cache compressed or non compressed pages.
    Manuel Lemos

    Metastorage - Data object relational mapping layer generator
    PHP Classes - Free ready to use OOP components in PHP


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •