[SOLVED] caching db query with file dependancy

Hi all,

A quick question, I think I’m completely misunderstanding the process of file dependency when caching, or doing something horribly wrong. How I understand it is that when caching an item with a file dependency like so:

[PHP]Yii::app()->cache->set(‘foo’, ‘bar’, 30, new CFileCacheDependency(’/myfile.txt’));[/PHP]

‘foo’ will remain in cache with the value ‘bar’ until;

a: the cache "time" expires (30 second in this case), or

b: the file is modified (i.e. saved)

However, running the above code and then on a separate page:

[PHP]$foo = Yii::app()->cache->get(‘foo’);

echo $foo;[/PHP]

This output’s ‘bar’ as you’d expect, however when I edit (or even delete) myFile.txt it still outputs ‘bar’, even though I’m not running the set() method again.

I’ve tried using an absolute and a relative path to the file, but with the same results, am I setting the dependency up wrong?

What I’m eventually trying to do is set a db query to use a file dependency to cache like so:

[PHP]$fileDependency = new CFileCacheDependency(’/myFile.txt’)

Category::model()->cache(60, $fileDependency)->findAll($criteria)[/PHP]

Any pointers to the right direction would be greatly appreciated,


bump Any hints at all as to why this may not be working? I still can’t figure it out, read through the http://www.yiiframework.com/doc/guide/1.1/en/caching.data#cache-dependency page a number of times :(

I don’t think that you are doing something wrong about caching …

However, one thing I’m not sure is your path to the file:

Do you have ‘/myfile.txt’ in your root directory?

Hi Softarjk,

Thanks for your reply, I do have a myfile.txt in my root directory (path\to\yii\project\myFile.txt), it’s sitting next to the project’s ‘index.php’ file, is there any way I can trace what the cache is actually doing to test where it (or me!) is going wrong?

‘/myFile.txt’ doesn’t point to ‘myFile.txt’ in your web root directory.

It points to a (probably non-existing) file that should be really located in your server’s root directory.

Please try something like this:

$filePath = '/path/to/yii/project/myFile.txt';

// check to see the file stat for debugging

Yii::trace(print_r(stat($filePath), true));

Yii::app()->cache->set('foo', 'bar', 30, new CFileCacheDependency($filePath));

I can’t think of other reason than an error in the file path. :)

Thanks, that’s what I was wondering if it’s a root path or not, I thought I’d tried that but obviously not, work now ta! :)