Yii Framework Forum: Caching static page content, but NOT dynamic content - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Caching static page content, but NOT dynamic content Rate Topic: -----

#1 User is offline   Emily Dickinson 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-September 10
  • Location:Albuquerque, NM

Posted 20 October 2010 - 12:44 AM

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
0

#2 User is offline   edanb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 16-June 09

Posted 22 October 2010 - 03:16 AM

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

1

#3 User is offline   Emily Dickinson 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-September 10
  • Location:Albuquerque, NM

Posted 23 October 2010 - 04:10 PM

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

Any other ideas, anyone?

Thanks!
Emily
0

#4 User is offline   galymzhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 27-October 10
  • Location:Алматы

Posted 13 November 2010 - 02:27 PM

How did you know that cache doesn't work? Did you configure cache component?
2b || !2b that's the question
0

#5 User is offline   yii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 25-July 09

Posted 24 November 2010 - 06:11 PM

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

Meine Fragen stelle ich öffentlich und baue somit eine Wissensdatenbank für andere auf!
1

#6 User is offline   Emily Dickinson 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-September 10
  • Location:Albuquerque, NM

Posted 04 December 2010 - 11:50 PM

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

0

#7 User is offline   Emily Dickinson 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-September 10
  • Location:Albuquerque, NM

Posted 04 December 2010 - 11:51 PM

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

#8 User is offline   galymzhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 27-October 10
  • Location:Алматы

Posted 05 December 2010 - 05:36 AM

> 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?
2b || !2b that's the question
0

#9 User is offline   Emily Dickinson 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-September 10
  • Location:Albuquerque, NM

Posted 05 December 2010 - 01:48 PM

View Postgalymzhan, on 05 December 2010 - 05:36 AM, said:

> 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().
0

#10 User is offline   lubosdz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 115
  • Joined: 25-July 10
  • Location:Slovakia, Bratislava

Posted 17 February 2011 - 11:24 AM

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
Yii extension: Captcha Extended

Greatest discoveries in 22nd century will be about the gravitation. | http://www.synet.sk | http://ipdf.sk
0

#11 User is offline   jgm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-July 11

Posted 28 September 2011 - 10:57 AM

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

#12 User is offline   DryKillLogic 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 14-December 10

Posted 29 November 2011 - 11:32 PM

Can it be excluded ajax requests?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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