Davey
(Davey975)
December 27, 2012, 9:42am
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
(Konapaz)
December 27, 2012, 10:13am
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);
}
Out of curiosity: Which cache component have you configured?
Davey
(Davey975)
December 27, 2012, 2:38pm
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
(Softark)
December 27, 2012, 3:12pm
5
Did you configure the "queryCacheID" of the db component?
Davey
(Davey975)
December 27, 2012, 7:26pm
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
(Softark)
December 28, 2012, 9:52am
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');
}
Davey
(Davey975)
December 30, 2012, 7:50pm
8
softark:
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');
}
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
(Softark)
December 31, 2012, 12:53am
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