Welcome Guest, Not a member yet? Register   Sign In
Help with dynamic caching

I need a caching system and the CI's one is pretty fast and helpful, it save about 50% of RAM resources.

However, there is something I still don't get.
How to not cache some data? for example, if someone is connected, he'll find he's username in the header... but since the file is cached and the header too, he won't see any difference.

In smarty, there are tags like {dynamic}{$username}{/dynamic}, how can we do that with CI ?


This question, or almost similar incarnations of it, seem to keep poping up. Have you tried using the search function? What you need is caching some data while keeping some other data dynamic, which is done by partial caching. You could do that using CI's database cache or using one of the user contributed libraries.

There's one in my sig (MP_Cache 2), there's multiple in de wiki (MP_Cache 1, KhCache of the top of my head) and Phil Sturgeon from PyroCMS wrote one.

Thanks !

I also have another question.
On my footer, I display random mysql rows, but on everypage so I'm not going to do the same request on each controller, is there a way to "include" a controller in the footer or to insert code in the footer... it's a view so I can't just query there.

Actually you could query from the view, though it's considered bad practice:
$ci =& get_instance();
$ci->db->query('YOUR QUERY');

The better way would probably be to put it in a library or model on its own and call it in the view like this (ie. not querying directly from the view):
$ci =& get_instance();
echo $ci->footers->random();

Hey Jelmer, thanks for the answer.

Why shouldn't I execute the SQL query directly from the view ?
If all my views are cached ?


[eluser]Phil Sturgeon[/eluser]
Executing SQL directly from the view goes against general MVC good practices.

Instead of making your views be the decider or whether a query is made, I use my Cache library to cache the actual data.

$data->footer_msg = $this->cache->model('footers', 'random', array(), 120); // 120 = 2 mins
$this->load->view('whatever', $data);

That keeps the data cached, then you can cache the view as you see fit. Keeps your code structured logically and in the MVC pattern. Models should get and set data, Controllers manage HOW the data is used and Views show the data. :-)

It's working as I want now, thanks a lot guys Wink

Theme © iAndrew 2016 - Forum software by © MyBB