views:

388

answers:

5

Hi,

I have a songs controller. Within the songs controller i have a 'view' action which get's passed an id, eg

  • /songs/view/1
  • /songs/view/5
  • /songs/view/500

When a user visits /songs/view/1, the file is cached correctly and saved as 'songs_view_1.php'

Now for the problem, when a user hit's a different song, eg /songs/view/2, the 'songs_view_1.php' is deleted and '/songs/view/2.php' is in it's place.

The cahced files will stay there for a day if I don't visit a different url, and visiting a different action will not affect any other action's cached file.

I've tried replacing my 'cake' folder (from 1.2 to 1.2.6), but that didn't do anything. I get no error messages at all and nothing in the logs.

Here's my code, I've tried umpteen variations all ending up with the same problem.

   var $helpers = array('Cache');
   var $cacheAction = array(
       'view/' => '+1 day'
   );

Any ideas?

EDIT:

After some more testing, this code

var $cacheAction = array(
    'view/1' => "1 day",
    'view/2' => "1 day"
);

will cache 'view/1' or 'view/2', but delete the previous page as before. If I visit '/view/3' it will delete the cached page from before... sigh

EDIT:

Having the same issue on another server with same code...

A: 

I think that kind of caching method is depreceted. Perhaps you should use Cache:

$song = Cache::read('songs/view/'.$id, 'cache_time');
if(empty($song)){
    $song = $this->Song->findById($id);
    Cache::write('songs/view/'.$id, $song, 'cache_time');
}

cache_time is a variable you define in core.php:

Cache::config('cache_time', array('engine' => 'File', 'duration' => 60*60*24));

Hope it helps.

metrobalderas
Caching the query result and caching a rendered page is not quite the same. When was page caching deprecated?
deceze
I said 'I think'.
metrobalderas
Well, then please don't say it without any reference. I was starting to wonder if I would be unable to update my sites to newer Cake versions, since I'm using page caching. :)
deceze
A: 

Check some setting in the config.php file. Do you have the following setting enabled?

Configure::write('debug', 0);
//Configure::write('Cache.disable', true);
Configure::write('Cache.check', true);
Cache::config('default', array('engine' => 'File'));
KienPham.com
Yep, I have those exact settings.
Jamesz
A: 

Hi I am facing the same problem... anyone has any solution yet?

Joe
+1  A: 

After working hours on this, I finally figure out the reason why the cache keep being deleted, the REASON is because you had some operations that update your 'song' record in the database after you view the 'song'. For my case, I keep a column in my database called 'Hits' to store the number of hits/reads, and it updates it everytime it read the record.

Cakephp has a feature to aumotically detect changes to your database and clear the cache for you.

Try remove any operations that update your 'song' record and the cacheaction should be working properly.

An alternative is to redefine the clearcache function in your 'song' model... it will disable the function to auto-clear off the cache.. but then remember to manually clear the cache yourself when an update is performed.

function _clearCache($type = null) {

} 
Joe
That works great, I couldn't get it to work when overwriting the clearCache function but I'll have another go when I've got more time.I was updating a field in the database that counts hits for each song and that was the problem.This has been frustrating me for months! I can't believe it was so simple. Thankyou.
Jamesz
A: 

Hi... I was a bit sleepy last night... miss out something important.... let me rephrase what I typed yesterday...

After working hours on this, I finally figure out the reason why the cache keep being deleted, the REASON is because you had some operations that update your 'song' record in the database after you view the 'song'. For my case, I keep a column in my database called 'Hits' to store the number of hits/reads, and it updates it everytime it read the record.

Cakephp has a feature to aumotically detect changes to your database and clear the cache for you.

Try remove any operations that update your 'song' record and the cacheaction should be working properly.

After fix it, there will be another issue, let's say you cache many of your records for eg song/1, song/5, song/100...etc, if there is any update for ANY 1 of the record... all of the caches for song/1, song/5, song/100 will be deleted. This makes cacheaction useless for frequently update website.

The solution to this is to redefine the clearcache function in your 'song' model... it will disable the function to auto-clear off the cache.. so if there is any update, none of the caches will be deleted. But then remember to manually clear the cache yourself when an update is performed.

function _clearCache($type = null) {

} 

to remove cache manually, you could use

@unlink(CACHE.'views'.DS.'website_songs_view_50.php'); 
Joe