Caching static page content, but NOT dynamic content

I’m writing an app whose main action, SiteController::actionIndex, interacts with a database.

However, all other pages of the app are text pages displaying static content, for example, an About page, a Help page, etc. These are very unlikely to change, so I’d like to cache them. These pages are accessible via

index.php/site/page?view=about

index.php/site/page?view=help

etc.

I’d like to cache the text pages, but not cache the content generated by SiteController::actionIndex.

It seems to me that the URL’s will vary by the “view” parameter. So, I tried to accomplish this task with this code, which fails:




public function filters()

{

    return array(

        array(

            'COutputCache',

            'duration'=>3600,

            'varyByParam'=>array('view'),

        ),

    );

}

I also tried variations on specifying the unique parameter set, such as those shown below, but none worked, e.g.:




   'varyByRoute'=>true,

   'varyByParam'=>array('page'),

   'varyByParam'=>array('page','view'),



This is surely something that many folks have tried to do, so if you can help me out, I would greatly appreciate it!

Emily

How about something like:




public function filters()

{

    return array(

        array(

            'COutputCache -index',

            'duration'=>3600,

        ),

    );

}



Or even better:




public function filters()

{

    return array(

        array(

            'COutputCache +page',

            'duration'=>3600,

        ),

    );

}



Nice, but didn’t seem to cache the content.

Any other ideas, anyone?

Thanks!

Emily

How did you know that cache doesn’t work? Did you configure cache component?

did you find an solution?

I also try to cache my static pages in my SiteController.php but it doesn’t work




	public function filters()

	{

		return array(

			array(

				'COutputCache +page',

				'duration'		=> 999999999,

			),

		);

	}

galymzhan > How did you know that cache doesn’t work? Did you configure cache component?

I can tell the page isn’t being cached by this method:

  1. Load the page in browser

  2. Modify the page locally, adding the word ‘FOO’

  3. Reload the page in browser. The change shows up, i.e., I see the word ‘FOO’.

galymzhan > Did you configure cache component?

Yes, I enabled it in my main config file, with:




'cache'=>array(

    'class'=>'system.caching.CFileCache',

    ), 



I’ve decided to just use the browser’s OWN cache, so the pages will be cached on the client and be way faster than any server-caching can possibly be.

I have another thread on this, and found a workable solution:

http://www.yiiframework.com/forum/index.php?/topic/13679-back-button-clears-inputs-on-previous-page/

To repeat just the part of that thread with a working solution, here it is:

The code below works with the latest MSIE and FireFox.

Place these lines just BEFORE any call to render():




$expires = 300;  // sec

header("Content-Type: text/html; charset: UTF-8");

header("Cache-Control: max-age={$expires}, public, s-maxage={$expires}");

header("Pragma: ");

header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');



:D

> I’ve decided to just use the browser’s OWN cache.

However it seems very strange, isn’t it? Since you are using file-based cache, have you checked a file which stores cached values?

Did you try using other caching components?

Regarding server-based file caching with CFileCache, I have that working. My main config file has:




'cache'=>array('class'=>'system.caching.CFileCache')



And that enables me to get files from the cache (assuming they were previously saved to cache with a cache key), e.g., with:




$myContent = Yii::app()->cache->get('cacheKey');



For my static content pages, it makes no sense to use server-based caching. The content changes so rarely, that it’s much faster to let the user’s browser cache the content. Hence the calls to header().

Hi,

you can exclude certain actions NOT to be cached for particular controller like this:




public function filters(){

    // exclude cache for certain pages (contact form, error page, captcha..)

    return array(

        array(

            'COutputCache -contact captcha error',

            'duration'=>3600,

            'varyByParam'=>array('lang', 'view'),

        ),

    );

}



It took me a while to figure this out…

Lubos

Thought I would updated this thread if any other newbie like me has this same issue as Emily. I too wanted to cache only my static files. I used the following filter to get things working…


    public function filters() {

        return array(

            array(

                'COutputCache + page',

                'duration' => 100,

                'varyByParam'=>array('view'),

            ),

        );

    }

The easiest possible way to check that your file caching is working would be to make sure that .bin files are created in the default cache location /protected/runtime/cache in your project directory.

Can it be excluded ajax requests?