How do I use caching

Discussion in 'PHP' started by wwwbryan, May 9, 2009.

  1. #1
    I made a script where there's a simple forum and tutorial posting feature. And I am planning on making a large site. So how would I implement a caching system. How does caching work. What are good tutorials out there for caching?
     
    wwwbryan, May 9, 2009 IP
  2. gemini181

    gemini181 Well-Known Member

    Messages:
    2,883
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    155
    #2
    gemini181, May 9, 2009 IP
  3. austin-G

    austin-G Peon

    Messages:
    435
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How large of a site do you plan on making? Unless it is handling thousands of requests per second, you probably won't need to cache.
     
    austin-G, May 9, 2009 IP
  4. fourfingers

    fourfingers Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not true, sites on shared servers can give you grief after 1000's / day ... not second.

    function make_content_file($filename,$content,$opentype) {
    	$fp_file = fopen($filename, $opentype);
    	fputs($fp_file, $content);
    	fclose($fp_file);
    }
    PHP:
    Use that to cache your output to a folder, then just check if a file exists BEFORE running any php on your page. If the page exists, just display the cache and bypass the php.

    If you can't figure this post out, there are tons of tuts out there. Just google it
     
    fourfingers, May 13, 2009 IP
  5. xxKillswitch

    xxKillswitch Peon

    Messages:
    331
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    A cache system I have implemented I found to be pretty easy to create. Basically, the idea is to capture the content and store it in a flat file for retrieval each time around. I decided to make a key for each item using md5 and stored it in an XML file.

    Before I call any content, I check if the cache file exists, if so I load that, use SimpleXML to grab the content and print it. Otherwise, I query the database and load info from there.

    I took it a bit further by making everything sort of a half flat file, half database driven site. When I create an article in the admin, it sends the info to the database but also creates the cached page. Upon editing an item, I allow the option to choose to update the cached copy.

    There is also an option not to flush the cache, to use it as a flat file. The cache is never flushed, and if it doesn't exist, it's created once the page is viewed.

    Send me a PM if you want the script.
     
    xxKillswitch, May 13, 2009 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    I think maybe more people would like a script like that - if possible, could you post it here, or on pastebin or something similar? Good resources should be shared ;)
     
    PoPSiCLe, May 14, 2009 IP
  7. xxKillswitch

    xxKillswitch Peon

    Messages:
    331
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Okay, I have inserted quite a bit of commenting to help those who find it useful get a grasp on using it. I am making a CMS that uses this, and it works very well. Short, simple and to the point.

    Some parts you may want to check error handling. I have an error logger, and had to remove those parts quickly so it didn't get confusing, which I think I replaced with return null or false.

    Read the comments, and if you find you need a live example let me know, I will make something really quick.
     

    Attached Files:

    xxKillswitch, May 14, 2009 IP