yBrodsky
yBrodsky

Reputation: 5041

CakePHP: Caching does not work

I am having the following issues: my cakephp app is not handling the cache thing properly. As suggested by every result in google, I created a function in the model to manually delete the cache:

public function afterSave($created) {
    Cache::clear();
    clearCache();
}

Unfortunately, this is doing nothing. Doesn't delete anything, and I still have the problem.

In case I have no explained myself properly, I will give an example of what happens:

I go with my browser to a page that shows a list of the last 5 records in my database. Then I go and add another record. I come back to the page that shows the last 5, and the information is not updated. It uses the cache and comes back with outdated info. If I press F5, then he page trully reloads and I see the trully 5 last records.

And that's it, I don't know what to do. The whole app works like crap, because you do something and it never appears unless you refresh the page with F5, which is something of course users are unaware, leading them to think "nothing was added" when it actually was.

Upvotes: 1

Views: 617

Answers (3)

Vaibhav Kumar
Vaibhav Kumar

Reputation: 544

Use of caching required lots of thinking, where to use where to not. If your update is frequent, don't use caching there.

We use caching where data rarely change, at that moment it is win-win situation.

Cache::clear($check, $config = 'default')

Destroy all cached values for a cache configuration.

cakephp Caching

Upvotes: 0

yBrodsky
yBrodsky

Reputation: 5041

I did this to solve the problem: In the controllers, inside beforefilter function I made a check, if the action is something I disable the cache.

The actions you choose won't have browser cache.

function beforeFilter(){
    if ($this->action == 'youraction'){
       $this->disableCache();
    }
}

Upvotes: 0

fsilva
fsilva

Reputation: 71

Cache::clear() will only clear entries that have expired.

Try Cache::clear(FALSE). Works if you have CakePHP 2.x.

Upvotes: 1

Related Questions