CodeIgniter Forums

Full Version: Codeigniter - cache only part of the view
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In my CI application I use to load differents parts of the view:

Code:
$data['header'] = $this->load->view('header', $data, true);

$data['footer'] = $this->load->view('footer', $data, true);

$data['user'] = $this->load->view('menu_user', $data, true);

$this->load->view('home_page',$data);

I would like to use the cache system:

$this->output->cache(60); // Will expire in 60 minutes
but the "menu user" for example, verifies every time if the user is logged or no. It doesn't need to be cached.

Is there a way for cache only some "part" of the page?
The best solution could be:

$data['header'] = $this->load->view('header', $data, true, 250); // 250 are minutes cache

$data['footer'] = $this->load->view('footer', $data, true, 60); // 60 are minutes cache

$data['user'] = $this->load->view('menu_user', $data, true);

$this->load->view('home_page',$data);


This would be an important feature for next version of CI.
That is very interesting. I have never used (or even knew about) this cache feature but it does appear to be a bit all or nothing. I think setting a cache for a particular view call could be interesting. However given that I did not even know about it I am not sure how important it actually is for me personally. Although I do have a site with a large dynamically generated menu that this might have been useful for. As it is I write a bit of code that once a day updates a separate table with the new menu, or updates the menu table should an admin change the feed data.

I can think of lots of places I might use a partial cache, but hardly any for a total cache.

So I think I agree with your point, but am unsure about how important it actually is.

Best wishes,

Paul.
first - my opinion is that you should not be verifying something as important as login state in a view file. that kind of verification should be happening in the controller -- and then you call the correct view and show the appropriate content.

the cache feature is awesome -- when you have concurrent users on the same web page. for many websites you are pulling a bunch of content from a database. by using the cache feature you completely eliminate those database calls. you then set up the time of the cache based on how often the page content changes. even if the content changes every 10 minutes it is still worth using cache, again to speed up the page response time when more then one person is looking at the same page.

i won't pretend to understand how the cache is created, but basically there is one file in the application/cache folder that is read to show the entire web page.
Have you seen or tried this?

http://www.codeigniter.com/user_guide/li...ching.html

You can use this to cache anything you want of an x amount of time.

Depending on your server you will need to choose a driver. The library does check if a driver is available when using the Cache interface. When directly using a cache driver you will need to check manually.
To play it save you can just always use the file driver.

How to use:
PHP Code:
// Load APC cache driver or fallback to file when APC not available
$this->load->driver('cache', array('adapter' => 'apc''backup' => 'file'));

// Get from / Save to cache
if ( ! $foo $this->cache->get('foo')) {
 
   // Cache 'foo' invalid or not set
 
   $foo 'foobarbaz!';
 
   
    
// Save into the cache for 5 minutes
 
   $this->cache->save('foo'$foo300);
}

echo 
$foo

It is that easy.
(12-14-2015, 03:58 AM)Martin7483 Wrote: [ -> ]Have you seen or tried this?

http://www.codeigniter.com/user_guide/li...ching.html

You can use this to cache anything you want of an x amount of time.

Depending on your server you will need to choose a driver. The library does check if a driver is available when using the Cache interface. When directly using a cache driver you will need to check manually.
To play it save you can just always use the file driver.

How to use:
PHP Code:
// Load APC cache driver or fallback to file when APC not available
$this->load->driver('cache', array('adapter' => 'apc''backup' => 'file'));

// Get from / Save to cache
if ( ! $foo $this->cache->get('foo')) {
 
   // Cache 'foo' invalid or not set
 
   $foo 'foobarbaz!';
 
   
    
// Save into the cache for 5 minutes
 
   $this->cache->save('foo'$foo300);
}

echo 
$foo

It is that easy.

Thanks, I will look this function