views:

446

answers:

5

Hi,

I was looking for HTML/Text content caching for small-mid size site using php. I'll mostly save the dynamic navigation-menu for site, generated HTML report from DB etc. Primarily I am looking for session based caching (is it a bad idea?). It can also be file based.

Any existing solution is much appreciated. For example Zend Framework is well known for its loosely coupled components. So, Zend_Cache can be a candidate, but could not find session based caching adapter. Moreover, it is not completely independent component. Can anybody tell what are the classes that I need to take to use Zend_Cache?

Another option is PEAR's - Cache_Lite, whats your take on this?

Is there any other framework, from where I can easily separate the caching component and use it with less learning curve?

Thanks.

A: 

You can look at the caching in CakePHP. I doubt that you will be able to separate it from the frame work but it should help you to understand how to cache dynamic content.

Lawrence Barsanti
+1  A: 

Session based caching is probably not a good idea. It's only appropriate in limited cases where you need to cache a specific result per-user (not for everyone).

APC is pretty widely deployed, so if you have access to it, I'd look into Zend_Cache with APC on the back end. If APC is not available, Zend_Cache with flat files on the back-end should be sufficient for small/medium type sites

timdev
+1  A: 

JPCache is a decent lightweight caching library.

Apemantus
A: 

Memcached comes to mind, as a really lightweight and efficient solution.

But you can also cache content in simple files. The filesystem is usually fast, and handles read/write locks without problems. And there's no need for any fancy library to handle that...the functions filemtime, file_put_contents and file_get_contents are all you need.

  1. Check if the cache has been written more than N secondes ago with filemtime()
  2. If it's too old, generate the content and write it with file_put_contents()
  3. If not, simply load it wit file_get_contents()

Edit: I'll add a link to that post I made a few months ago : Best Solution for caching. It's not completely on topic, but it might help you in your researchs :)

Nicolas
A: 

Most of the php caching libraries are implemented using the output buffer control functions. You can implement your own very simple caching the same way.

<?php

function callback($buffer)
{
  // Code to store output in cache
}

if (/* Test cached copy is still valid */) {
    /* Output cached copy to browser */
    exit(0);
}

ob_start("callback");
?>

<html>...</html>

<?php
ob_end_flush();
?>

You can omit the ob_end_flush() if you like, since it will be triggered automatically at the end of the output.

The interesting thing to note is that this structure could be wrapped around smaller units than the page. For example you mention caching just the navigation menu. You'd need a bit more logic around the block to be cached, but the principle is the same.

Mike Houston