Yii Framework Forum: Fragment Caching question - Yii Framework Forum

Jump to content

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

Fragment Caching question Rate Topic: -----

#1 User is offline   BornToDrink 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 31-December 09
  • Location:Bulgaria

Posted 05 July 2010 - 06:21 AM

Hello.

I am using fragment caching for some categories printing, so this is the example code:

if($this->beginCache('catsTree', array('duration'=>800)))
{
    //print cats	    
    $this->endCache();
}


And everytime I reload the page $this->beginCache return true.. which is unexpected result.
I tested it with simple changing of content in "print cats" code and it seems no cache is available.

Am I doing it wrong or missing something?

Yii version is 1.1.2

Thanks!
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 05 July 2010 - 06:33 AM

Did you configure a cache component in your main.php? E.g. APC:


    'components'=>array(
        'cache'=>array(
            'class'=>'CApcCache',
        ),

1

#3 User is offline   BornToDrink 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 31-December 09
  • Location:Bulgaria

Posted 05 July 2010 - 07:51 AM

Oh.. of course I missed something...
Thanks man.

While we are in cache topic, I found this situation:

include(Chtml.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

Wrong typed CHtml (with small "h") was after cached section in the same view file.

I fixed it, but is there a reason that this error hasn't appeared before adding "if($this->beginCache...."? :)

Maybe CHtml was included already, because it was used in cached section.
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 05 July 2010 - 07:56 AM

Are you on Windows? My guess then would be: The filesystem itself is case independent, so if you use pure PHP everything wents fine. But APC is more sensible with upper lower cases of cached class files.

Just make sure your filenames always matches exactly and you should never have a problem on Windows AND Linux.
0

#5 User is offline   BornToDrink 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 31-December 09
  • Location:Bulgaria

Posted 05 July 2010 - 08:04 AM

View PostMike, on 05 July 2010 - 07:56 AM, said:

Are you on Windows? My guess then would be: The filesystem itself is case independent, so if you use pure PHP everything wents fine. But APC is more sensible with upper lower cases of cached class files.

Just make sure your filenames always matches exactly and you should never have a problem on Windows AND Linux.

No, it's Linux hosting.
I mean that when include files in Case Sensitive OS/server, this is a problem, but when just call CHtml twice (second one with lower case like "Chtml") it's working and in my situation this was unexpected issue, because cache section actually hide part of my PHP code (including CHtml method). :)
However, the problem is fixed and the cache is working now. :)
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 05 July 2010 - 08:06 AM

Ah, right. That explains it pretty good.
0

#7 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 05 July 2010 - 08:48 AM

Since the name of this topic is pretty general, I would like to ask one more question about fragment caching :)
In my application I retrieve some information from other site every 3 hours, insert it into database and make Yii::app()->cache->flush() to update a block on a site containing this information. Since it is the only block being cached, I can flush all cache info. But what if I have many cached blocks and I want to flush only one block's cache? I tried:

Yii::app()->cache->delete('BlockId');

// or

Yii::app()->cache->delete('Yii.COutputCache.BlockId');

but both lines don't delete fragment cache file. I did a quick look at how file name is generated and it looks more complex than previous two lines.

Any tips? :) Thanks.
0

#8 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 05 July 2010 - 09:18 AM

The way this is supposed to happen is by using a CCacheDependency, i think. Maybe a CGlobalStateCacheDependency helps here.
0

#9 User is offline   BornToDrink 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 31-December 09
  • Location:Bulgaria

Posted 05 July 2010 - 01:34 PM

Hi again.
I have another problem.
When use cache for fragment of sitewide view (like sidebar), cache file is not the same for different site links.
For example, I add a link in sidebar and this link appear in one page, but on the index for example is missing (because of cache).
And on the page where exist is actually cached too, coz if i remove the link, it appear on this page during cache duration.
Any idea why this is happened? :)

Thanks!
0

#10 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 05 July 2010 - 02:37 PM

@BornToDrink:
From what i understand, you should take a look at caching dynamic content. Like: Most parts of your cached content (sidebar) stay the same, except for some dynamic parts (some links...)


P.S.: Maybe a better title for this topic would be "Everything you always wanted to know about caching* (*But where afraid to ask)" ;D
0

#11 User is offline   BornToDrink 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 31-December 09
  • Location:Bulgaria

Posted 05 July 2010 - 03:19 PM

:D
Thank you for joining this topic again.

But let me explain what exactly is the problem (I reproduced the issue with demo blog).

So to reproduce this issue, please follow these steps:
1. Download Yii (1.1.3 is OK)
2. Open demos/blog/protected/config/main.php and add these lines in "components" section:
'cache'=>array(
    'class'=>'CFileCache',
),

3. Open demos/blog/protected/views/layouts/column2.php and add these lines:
if($this->beginCache('somecache', array('duration'=>600)))
{
	echo time();
	$this->endCache();
}

4. After save both files, open home page and see the timestamp in sidebar, now go to "Comments" link on first blog post - timestamp is different!

Now you can click on index and "Comments" pages to see that we have different cache fragments.. (timestamp is not changed until 600 secs, but in both pages timestamp is different)... which is the issue in my case. :)
Is this again configurable issue or it's a bug? :)
0

#12 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 06 July 2010 - 02:40 AM

Interesting :). Would not have that expected that either.

beginCache() uses the COutputCache widget internally. It has some varyBy* properties. They define the condition, when another fragment should get cached even for the same id. And here you see that varyByRoute defaults to true. The problem should be gone if you use:

if($this->beginCache('somecache', array('duration'=>600,'varyByRoute'=>false)))

1

#13 User is offline   BornToDrink 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 31-December 09
  • Location:Bulgaria

Posted 06 July 2010 - 05:46 AM

Ahh.. :)
Now everything is OK. Thanks again. :)

Yesterday I made some tests with Apache Bench and the caching technique improve the performance on my application with about 10-25%, depending on current page. Which is really nice. :)
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