Welcome Guest, Not a member yet? Register   Sign In
Caching based on content blocks

Hello everyone.

I'm looking for a way to cache my output, before gathering data. CI seems to support per-page caching only, which won't do in this situation. Imagine a contentpage, which contains various blocks. Each block has it's own template and thus each block has its own cache with it's own lifetime.

I've thought of the solution below, and I'm quite content with its functionality. What bothers me is the fact that I need to call and check for cache in every template-returning method.

The question: can anyone think of a way to automate this call?


class HomepageController {
  // Gather data for homepage
  public function index()
    $data['images']   = $this->images( );
    $data['content']  = $this->content( );
    $this->load->view( 'homepage', $data );
  // Pseudo code, fetch images
  public function images( )
    if ($this->ib_cache->is_cached( 'homepage/images', 14 ) )
      return $this->ib_cache->get_cache( 'homepage/images' );
      $this->load->model( 'ImageModel' );
      $data['images']    = $this->ImageModel->get_newest_images( 5 );
      return $this->load->view( 'images', $data, true );
  // Pseudo code, fetch content
  public function content( )
    if ($this->ib_cache->is_cached( 'homepage/content', 8 ) )
      return $this->ib_cache->get_cache( 'homepage/carousel' );
      $this->load->model( 'ContentModel' );
      $data['content']    = $this->ContentModel->get_page( 'home' );
      return $this->load->view('content', $data, true);

Thanks in advance

You can try creating an extended Controller library and create your actual controllers to extend the extended controller.

In your libraries :
class My_Controller extends Controller {

  function My_Controller() {


In your controllers directory :
class My_application_controller extends My_Controller {

If you have a look on the forums I remember seeing a cache library. What it did was let you cache objects and such to files with a keyname. You could then pull them back out of cache to output. Sadly I cannot find the library or the post about it atm.

This would do what you wanted though I believe.

I think you may be referring to Al James' Sparks Library.
He also made another cache library.


Thank you very much, I'll look into that.

Theme © iAndrew 2016 - Forum software by © MyBB