Activerecord Cache


(Davey975) #1

I’m a bit messing around with the cache option for Yii, but i have a question. When i use the ActiveRecord cache, like:

$series = Post::model()->cache(3600)->getPopularPosts(5);

I see in my webLogRoute that it only saves the cache every time, but it never loads it from the cache, why not?


(Konapaz) #2

Hi Davey

check this


$id = 'unique-your-getPopularPosts';

$series = Yii::app()->cache->get($id);

if($series === false)

{

    $series = Post::model()->getPopularPosts(5);

    Yii::app()->cache->set($id, $series, 3600);

}


(Da:Sourcerer) #3

Out of curiosity: Which cache component have you configured?


(Davey975) #4

Thanks, will give that a try!

I’m using CMemCache btw

Edit:

Did try it on this way, but it’s the same story, on every reload the query is executed and saved in cache (it’s never ‘loaded’ from the cache):

"Saving "serielist-popular" to cache"

Is there something wrong with my configuration?




'cache'=>array(

            'class'=>'CMemCache',

            'servers'=>array(

                array(

                    'host'=>'localhost',

                    'port'=>11211

                ),


            ),

       ),


(Softark) #5

Did you configure the "queryCacheID" of the db component?


(Davey975) #6

Tried that, still the same result. I think it shouldn’t matter, because the cache->set(); cache->get() method didn’t work for me neither and that has actually nothing to do with the database itself (just the results got cached)


	'db'=>array(

	       /* Secret stuff here */

			'emulatePrepare' => true,

			'charset' => 'utf8',

            'enableProfiling' => false,

            'queryCacheID' => 'cache'

		),


(Softark) #7

I see.

It might be worth trying the basic CFileCache instead for debugging.

And, I’m a bit afraid that you were misunderstanding something when you were interpreting the log. I mean, the cache might be working right. You would be better to make sure it IS failing.




$id = 'unique-your-getPopularPosts';

$series = Yii::app()->cache->get($id);

Yii::trace('read from cache : ' . ($series === false) ? 'false' : 'cached' );

if($series === false)

{

    $series = Post::model()->getPopularPosts(5);

    Yii::app()->cache->set($id, $series, 3600);

    Yii::trace('write to cache');

}




(Davey975) #8

Thanks! With CMemCache i get ‘write to cache’ every time in my trace log. With CFileCache instead, i get what i expect in my log, ‘Serving “NAME*” from cache’ the second time i load my page, so that works as it should.

But i would like to use Memcache anyway, what can be the problem? A server side setting?


(Softark) #9

Yes, probably.

And if you are using PECL::memcached extension for interfacing with memcached, the doc says you have to set ‘useMemcached’ to true.

http://www.yiiframework.com/doc/api/1.1/CMemCache#useMemcached-detail