Fragment Cache Conditionally

Hi

Is there a yii-way to avoid caching for specific requests on the same controller/action ?

For example I have an action that may have a sorting parameter in the url

(I want cache all categories)

http://www.mydomain.com/index.php?r=site/categories/3 (main request category=1,2…)

(I don’t want cache all categories by sorting)

http://www.mydomain.com/index.php?r=site/categories/3&sort=desc (specific request category=1,2… with sorting)

Because there are a lot of combination of categories and sorting, I want to cache only the main cases of categories,

I found requestTypes in COutputCache class to exclude caching but this is only for the type Request (GET,POST)

So, How to exclude the caching for sorting?

P.S. I could have this code


$exclude = isset($_GET['sort']);

$c = false;


if (!$exclude) {

$c = ($this->beginCache('an_id', array('varyByParam' => array('cid')));

}

//

//---- the content ----

//

if ($c) {

$this->endCache();

}

But I don’t know if its tricky way…

Hi, I think you must use CExpressionDependency. Something like this:




$a = true;

if(isset($_GET['sort'))

   $a = false;

if($this->beginCache('cache_id', array('dependency'=>array(

   'class'=>'system.caching.dependencies.CExpressionDependency',

   'expression'=>"{$a} === true"<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/wink.gif' class='bbc_emoticon' alt=';)' />))) { ?>

...content to be cached...

<?php $this->endCache(); } ?>



Hi Masoud

Unfortunatly not works…