writing a cache class, and need some 2nd thoughts.

Discussion in 'PHP' started by l3vi, Jan 24, 2008.

  1. #1
    Im working on a website cache class. Since the site can not trigger a cache on the output of the content, we are going to setup a class that runs on cron x number of minutes, hours, or whatever a day and opens a http connect to the pages that we want cached, to get html content, then save it to a file, which at that point the pages that use that cache will be able to load it if the cache has not expired.


    So, while doing all this is easy Im trying to think how I should keep the time frame of tracking with this. Should I just write the files into a folder with a number or time stamp or something, or once more use a database to load up the cache for that page and then full the file from the name.

    The database sounds like the best for flex, however that means once more processing and mysql logic which = load...

    So if you have exp on this topic I would love to hear what you did to handle caching, updating, etc of the site.
     
    l3vi, Jan 24, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Hi,

    The easiest way to get the date/time a file was updated, is to use stat(). It will return created, modified and access, amongst other statistics (that you would get on a typical system). As it returns UNIX Timestamps, it easy to compare the modification date of that file with time() and see if its too old to be used.

    Hope this helps,

    Jay
     
    jayshah, Jan 24, 2008 IP
  3. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If it is straight html files you could always save them as static html-files to be served by Apache (or whatever webserver you use). And have the cronjob update the files at a set interval.

    Why do you need a time for when the file was last updated? If you update it with a cronjob each time anyway?
     
    tamen, Jan 24, 2008 IP
  4. sharqi

    sharqi Guest

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It all rather depends.

    Like tamen says, if you are happy with having the page updated every xyz minutes then this is really a non issue. Set cron, get pages, save pages, serve pages.

    If you wish to have the content update dynamically based on critera then its an open ended subject, depending on what exactly the conditions that trigger an update are.

    You can use stat mtime (like jaysha says) if the files you are caching are coming from somewhere you have access too. If you are simply taking pages from a public web server then its not going to be much use to you regarding checking new content - of course it can be useful to see how long it has been since you have last updated a given file on your server, and without resorting to using an over engineered database solution.

    I would suggest that you use the non DB approach, as if written correctly it would be a breeze to add database functionality later if scale increases drastically and/or you want to start data-mining etc.
     
    sharqi, Jan 25, 2008 IP