[eluser]CrossMotion[/eluser]
I am working on a codeigniter project where I want to have a page build from multiple views of witch only a few are not cached. I am building my controller as following:
Code: <?php
class Home extends Controller {
function Home()
{
parent::Controller();
}
function index()
{
$this->load->helper('text');
$this->load->model('mod_widgets');
$this->load->model('mod_general');
$categorieen['categorieen'] = $this->mod_widgets->fetch_categories();
$nieuw['nieuw'] = $this->mod_widgets->fetch_new_widgets('4');
$populair['populair'] = $this->mod_widgets->fetch_populair_widgets('4');
$aanbevolen['aanbevolen'] = $this->mod_widgets->fetch_recomended_widgets('6');
$advert['advert'] = $this->mod_general->fetch_random_advert('large_rectangle');
$contentblocks = $this->load->view('content/widgets_aanbevolen',$aanbevolen,true);
$contentblocks .= $this->load->view('content/widgets_populair',$populair,true);
$contentblocks .= $this->load->view('content/widgets_nieuw',$nieuw,true);
$contentblocks .= $this->load->view('content/advert_rect',$advert,true);
$data = array(
'title' => 'Homepage',
'login_block' => $this->load->view('layout/layout_login_block','',true),
'menu' => $this->load->view('navigation/nav_menu','',true),
'categorieen' => $this->load->view('navigation/nav_cat',$categorieen,true),
'content' => $contentblocks,
'footer' => $this->load->view('layout/layout_footer','',true)
);
$this->load->view('home',$data);
}
}
/* End of file home.php */
/* Location: ./system/application/controllers/home.php */
In this case I would like to cache everything except the login_block. and then of course the global view. the other subvie's can all be cached.
Is there a way I can do this, or does someone know a good tutorial featuring partial caching?
[eluser]CrossMotion[/eluser]
I understood that Fragment Caching was a part of the CodeIgnitor framework, but since No one replied till now I guess I was mistaken... Is there someone who can confirm this for me?
Also I think this is a quite common problem since most current websites have things like shopping carts and other personal content that we don't want cached.. I am really curious how other people have tackled this problem.
[eluser]Jelmer[/eluser]
There's no fragment caching in CodeIgniter, there's 2 possible solutions for your problem though:
- Database query caching: standard functionality, look in the User guide
- MP_Cache : a class I wrote for a similar purpose, which would allow you to cache any part of your controller you like.
With my library you'd rewrite the index function to something like: Code: function index()
{
$this->load->helper('text');
$data = $this->mp_cache->get('example');
if ($data === false)
{
$this->load->model('mod_widgets');
$this->load->model('mod_general');
$categorieen['categorieen'] = $this->mod_widgets->fetch_categories();
$nieuw['nieuw'] = $this->mod_widgets->fetch_new_widgets('4');
$populair['populair'] = $this->mod_widgets->fetch_populair_widgets('4');
$aanbevolen['aanbevolen'] = $this->mod_widgets->fetch_recomended_widgets('6');
$advert['advert'] = $this->mod_general->fetch_random_advert('large_rectangle');
$contentblocks = $this->load->view('content/widgets_aanbevolen',$aanbevolen,true);
$contentblocks .= $this->load->view('content/widgets_populair',$populair,true);
$contentblocks .= $this->load->view('content/widgets_nieuw',$nieuw,true);
$contentblocks .= $this->load->view('content/advert_rect',$advert,true);
$data = array(
'title' => 'Homepage',
'menu' => $this->load->view('navigation/nav_menu','',true),
'categorieen' => $this->load->view('navigation/nav_cat',$categorieen,true),
'content' => $contentblocks,
'footer' => $this->load->view('layout/layout_footer','',true)
);
$this->partial_caching->write($data, 'example');
}
$data['login_block'] => $this->load->view('layout/layout_login_block','',true),
$this->load->view('home',$data);
}
Where you might change the cache name from 'example' to a variable which identifies the current page.
[eluser]CrossMotion[/eluser]
Sounds very cool, you did a nice job on this!
Only I have a lot of page view counters showing how much times an item (in this case a widget) is viewed. I don't want to delete the cache every time one widget is viewed, but more like once per hour.
So I need to delete the cache after a set amount of time. Do you have an idea how I could achieve this?
Thx in advance!
Dennis
[eluser]Jelmer[/eluser]
I actually haven't added any cache-management options in the library. In my own implementation most of my cache files correspond to a certain query or set of queries and I delete them after any edit.
To achieve what you want you could do it like this:
Code: $data = $this->mp_cache->get('example');
if ($data === false OR (time() - $data['generation_time']) > 3600)
{
// the code for the data array
$data['generation_time'] = time();
$this->partial_caching->write($data, 'example');
}
Or you could create seperate caches for every widget. That would make your code a lot less readable, but also eliminate lots of duplicate cache and you could have seperate "re-caching" rules for every part of the page.
|