Yii Framework Forum: Activerecord Cache - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Activerecord Cache Rate Topic: -----

#1 User is offline   Davey 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 14-September 11

Posted 27 December 2012 - 04:42 AM

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?
0

#2 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,317
  • Joined: 21-February 11
  • Location:Greece

Posted 27 December 2012 - 05:13 AM

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);
}

Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 27 December 2012 - 06:42 AM

Out of curiosity: Which cache component have you configured?
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#4 User is offline   Davey 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 14-September 11

Posted 27 December 2012 - 09:38 AM

View PostKonApaz, on 27 December 2012 - 05:13 AM, said:

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);
}



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
                ),

            ),
       ),

0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 27 December 2012 - 10:12 AM

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

#6 User is offline   Davey 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 14-September 11

Posted 27 December 2012 - 02:26 PM

View Postsoftark, on 27 December 2012 - 10:12 AM, said:

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


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'
		),

0

#7 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 28 December 2012 - 04:52 AM

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');
}

0

#8 User is offline   Davey 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 14-September 11

Posted 30 December 2012 - 02:50 PM

View Postsoftark, on 28 December 2012 - 04:52 AM, said:

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?
0

#9 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 30 December 2012 - 07:53 PM

View PostDavey, on 30 December 2012 - 02:50 PM, said:

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

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.yiiframew...emcached-detail
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users