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

[eluser]kakawett[/eluser]
Hi.
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 ?

thanks
#2

[eluser]Jelmer[/eluser]
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.
#3

[eluser]kakawett[/eluser]
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.
#4

[eluser]Jelmer[/eluser]
Actually you could query from the view, though it's considered bad practice:
Code:
$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):
Code:
$ci =& get_instance();
echo $ci->footers->random();
#5

[eluser]kakawett[/eluser]
Hey Jelmer, thanks for the answer.

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

Thanks
#6

[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.

Code:
$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. :-)
#7

[eluser]kakawett[/eluser]
It's working as I want now, thanks a lot guys Wink




Theme © iAndrew 2016 - Forum software by © MyBB