Welcome Guest, Not a member yet? Register   Sign In
Problems with output cache
#1

[eluser]davste[/eluser]
Hi,

I use the Output cache method on some of my pages:
$this->output->cache (60);

This works fine, but whenever the user logs out the system, and accesses the restricted page, the function's constructor won't redirect him. The thing that happens is that the Output Class will get the Cache file and display that.

Code:
class Restricted_page extends Controller {

public function __constructor ()
{
    parent::Controller();

    if ($this->Authentication->login_validation() === FALSE) {
        redirect ();
    }
}

public function index ()
{
    $this->output->cache (30);

    $this->load->view ('admin_index');
}
}

This is just an example code, but gives you an idea. Instead of being redirected by the Constructor, the cached page will show.. I want the constructor to run, and THEN load the content from cache, if the user is logged in!
#2

[eluser]mikelbring[/eluser]
Did you try to put exit() after the redirect?
#3

[eluser]davste[/eluser]
Should be no need: it's in the redirect() function as standard.

I'm thinking the Output class bypasses the whole controller, am I right? If so, that would explain why my redirect never happens.
#4

[eluser]Twisted1919[/eluser]
The cache() method from output class is useless .
Try and use a caching lib , as it will get you more control over what you are showing to the end user .
#5

[eluser]Jelmer[/eluser]
[quote author="davste" date="1266963720"]I'm thinking the Output class bypasses the whole controller, am I right? If so, that would explain why my redirect never happens.[/quote]

Yes, you're right. Checking for cache happens before anything of any importance is executed. On every pageload CI checks whether there's a cache for the URI requested, when it's there the cache is loaded and displayed and that's it.

The output cache is indeed worthless to you when you need anything that might be dependent on the current visitor. You can look into query caching with Active Record or take a look at my Cache library for example (it's in my sig).
#6

[eluser]cahva[/eluser]
I agree with Twisted1919. CI's own cache is quite useless. I wouldnt even dare to use it if I had some user driven site(CI could cache someone elses profile page for example).

I can warmly recommend Phil Sturgeons Codeigniter-Cache. With that its easy to cache what you need to cache.
#7

[eluser]n0xie[/eluser]
Could you elaborate why you think the CI cache is 'useless'? We use it extensively and I dare see it is far from useless to us.
#8

[eluser]cahva[/eluser]
Ok, I was a little harsh. I meant its useless on user-driven sites where content changes according to user.
#9

[eluser]n0xie[/eluser]
[quote author="cahva" date="1266983649"]Ok, I was a little harsh. I meant its useless on user-driven sites where content changes according to user.[/quote]
That depends entirely on your application and/or the way you set it up.

Since the output library caches based on URI, the chance of having a profile showing up from the wrong user with the url scheme domain.tld/profile/id is highly unlikely. For most other 'widget type' changeable content, AJAX is usually the way to go.

Most high traffic sites would not survive without static html caching. I'm not saying the CodeIgniter Cache is the best implementation ever, but I would hardly call it useless. Remember you can tweak the TTL of the cache making it so that pages that change a lot (say the homepage) gets refreshed often while others remain in cache for a long time.

Keep in mind that in most applications, the round trip to the database is the most expensive (i.e. the bottleneck) so anything that lowers the hit on your RDMS, will most likely dramatically improve the performance of your website.
#10

[eluser]cahva[/eluser]
We've just published an ecommerce site which has ofcourse 'widget type' content. We dont want to rely too much on javascript and AJAX. Plus we use session to keep track of the user on the site, not userid in url-segment.

But plain and simple, it was just easier to use Phil Sturgeons cache lib where we could control what to cache and what not to cache. We first tried to use CI's own cache but we came to conclusion that it would be too much hassle to get it to work like we wanted.

Its also easy to remove particular cache-files from cache, which is a little hard to do as you dont have fine-grained control over CI's cache files. For example in the ecommerce site we did, we have separate cache files for navigation, product and categories page + other site content. There are parts that can live longer in the cache but if necessary, its easy to remove them as needed.

Im sure CI's simple pagecache works and is good for maybe 85-95% of sites but for our project it was a no-no Smile




Theme © iAndrew 2016 - Forum software by © MyBB