2

Good sunday everyone! I just started playing with codeigniter, up to now everything seems so clear (great docs!), but i'm trying to understand how works $this->output->cache().

What i wanto to do is to cache only some component of my interface. For example, in my controller i load a view for each part of the structure: header_view, topnav_view, sidebar_view and home_view.

Using $this->output->cache(n) in my controller i get cached the whole page. What if i want to cache the whole page except the header_view?

Luciano
  • 1,455
  • 8
  • 22
  • 52

2 Answers2

1

Use Phil Sturgeon's cache library to cache as much (or as little) of the page as you want.

I generally just use the get/write/delete functions to cache the non-user-specific database reads (from multiple model calls) for my pages. But do consider the model/library and dependency functionality if it works in your case

coolgeek
  • 903
  • 6
  • 8
1

CodeIgniter Reactor (2.0) has that feature built-in. I suggest you use CodeIgniter Reactor's latest release.

CodeIgniter Reactor is the community branch of the somewhat - until recently - neglected project from EllisLab. It is officially sponsored with "community stewards" who guide the development and ensure that the coding standards are followed. It contains many new features and is much farther along than previous releases of CodeIgniter (1.7.3). Yet it's cut from the same cloth so to speak as it is a branch of the official CI source.

sholsinger
  • 3,028
  • 2
  • 23
  • 40
  • @sholsinger: thanks for your reply! I'm already using CI 2.0 downloaded from the official site, is there any difference between codename Reactor? Anyway looking at the article from Greg Aker i understand that i must not cache the entire view, only the array with data i'm sending to the view... is it right? – Luciano Mar 07 '11 at 18:13
  • 1
    Correct. You just give it a key, such as `'all-users-db-result'` and the value, `$all_users_result` and it will cache it using whatever caching driver you've configured it to use. Reactor is simply the community branch which most development occurs in. EllisLab will merge some or all of the Reactor code back into the CodeIgniter release. – sholsinger Mar 07 '11 at 22:28
  • @Luciano having said that, you _could_ cache the view if you want. But it's not that _expensive_ in terms of server time. The database call takes much longer than rendering the view. – sholsinger Mar 08 '11 at 14:05
  • @sholsinger: Thanks, really helpfull. I'm just testing on it to understand which data to cache for less elapsed_time and memory_usage... i've tried caching the whole output and the queries, that worked really good! Now i'm just going to try with APC, i want that only some part of the views get cached... your instruction seems to be perfect for that. Thanks! :) – Luciano Mar 08 '11 at 18:47
  • 1
    @Luciano You can cache individual views (or partial views) like this: `$this->cache->save('my-partial-name', $this->load->view('partial-name', $data, true))` – sholsinger Mar 09 '11 at 16:00
  • @sholsinger: great, another good suggestion! Have you got any experience with [CI and datagrid](http://stackoverflow.com/questions/5245795/codeigniter-using-datagrid)? – Luciano Mar 09 '11 at 17:55