CodeIgniter Forums
Caching based on content blocks - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Caching based on content blocks (/showthread.php?tid=5202)



Caching based on content blocks - El Forum - 01-11-2008

[eluser]Unknown[/eluser]
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?

Code:
<?php

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' );
    else
    {
      $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' );
    else
    {
      $this->load->model( 'ContentModel' );
      $data['content']    = $this->ContentModel->get_page( 'home' );
      
      return $this->load->view('content', $data, true);
    }  
  }
?>


Thanks in advance


Caching based on content blocks - El Forum - 01-11-2008

[eluser]Crafter[/eluser]
You can try creating an extended Controller library and create your actual controllers to extend the extended controller.

In your libraries :
Code:
class My_Controller extends Controller {

  function My_Controller() {
     parent::Controller();
     all_other_functions();
  }

}

In your controllers directory :
Code:
class My_application_controller extends My_Controller {
}



Caching based on content blocks - El Forum - 01-12-2008

[eluser]adamp1[/eluser]
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.


Caching based on content blocks - El Forum - 01-12-2008

[eluser]Sarre[/eluser]
I think you may be referring to Al James' Sparks Library.
He also made another cache library.

greetz


Caching based on content blocks - El Forum - 01-14-2008

[eluser]Unknown[/eluser]
Thank you very much, I'll look into that.