Difference between #5 and #6 of
caching controllers with filters()

Changes

Title unchanged

caching controllers with filters()

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

cache, page, invalidate, flush

Content changed

Hello, this article will describe some aspects about caching your controllers with filters() method in your controller. And iI will describe some problems which iI meet when useding this approach for caching, the main problem was ### how can i invalidate a cache for specific action?
 
I googled the forums [how to delete whole-page cache from cache?](http://www.yiiframework.com/forum/index.php?/topic/14533-how-to-delete-whole-page-cache-from-cache/ "")
 
[flush controller cache](http://www.yiiframework.com/forum/index.php?/topic/17459-flush-controller-cache/page__p__86457#entry86457 "")
 
[Is there any way to invalidate cached version of a whole-cached page?](http://www.yiiframework.com/forum/index.php?/topic/14645-is-there-any-way-to-invalidate-cached-version-of-a-whole-cached-page/page__p__72990__hl__cache+filter+#entry72990 "")
 
asked on IRC and SO but iI didn't find an answer, therefore decided to write this article. Our first task is to invalidate a cache for specific page. When you use filters() and COutputCache in your controller you will find some restrictions of this approach. 1. If you change your filters() caching settings, you need to invalidate your cached data, before your filters() changes will be in action. 2. You can't invalidate a specific page or specific action, and sometimes you don't need and don't want to flush whole cache, for example: if you're changing your duration option. 3. If you change some dependency in your filters() you also don't need and don't want flush whole cache. Yii doesn't have an implemented function for this and i need to make it. At first we need to create our own Cache Component.
 
Note: I will use CFileCache component, because i use CFileCache as my main cache. But it mustshould work with every cache class. I created MyCache.php file in ./protected/components/
 
 
```php class MyCache extends CFileCache {     public function getValue($key) {
 
        if( isset( $_GET['flush'] ) )
 
            
$this->deleteValue($key);
 
        return parent::getValue($key);     }
}
[...]
```php
'cache'=>array(
    //'class'=>'system.caching.CFileCache',     'class'=>'MyCache', ), ``` That's all. Now when you want to invalidate a specific page cache, you just need to add a flush variable into your URI request. ex: i want tTo invalidate just index page from mythe cache, i use nextthis url: www.myyiisite.com/?flush   . That's all, index page is invalidated and new cache settings is now in action. Drowbacks: 1. I can't invalidate a controller cache. If you have 10 controllers in your app, and changed your filters() code, you don't want to flush whole cache. You want to invalidate the cache just for this controller. And Yii can't.
 
 
2 0
8 followers
Viewed: 18 148 times
Version: 1.1
Category: How-tos
Written by: RusAlex
Last updated by: Maurizio Domba Cerin
Created on: Mar 27, 2011
Last updated: 13 years ago
Update Article

Revisions

View all history