[Solved] Coutputcache: Prevent Flash Message From Getting Cached?

I’m caching my pages using COutputCache which caches the entire view response. I have CDbCacheDependency to invalidate the cache when data changes. This is working fine. However, I just noticed a problem in that sometimes flash messages are getting cached and displayed to all users. For example I’m caching a single post view. If a user adds a new comment to that post the cache gets invalidated and a new cache is formed. However this new cache will contain the flash message such as “Thanks for your comment”.

Any thoughts on how to prevent this?

You could render the flash message as dynamic content. See here:

http://www.yiiframework.com/doc/guide/1.1/en/caching.dynamic

That’s good to know looks almost exactly like what I’m looking for. The downside is my flash message are currently displayed in my layout view which is used by several controllers and actions. The flash messages are pretty much restricted to a single view at this point. So I’d rather not introduce the dynamic content on every view just so it can handle one view. I know that doesn’t fully make sense because if I’m only using the flash messages on one view then I could just move the flash messages out of the layout view and into that particular view.

The other alternative I guess is to not cache the entire action response for this one particular view and instead cache some of the queries it uses.

Also wondering if I could use a CChainedCacheDependency with my current CDbCacheDependency and also adding a CExpressionDependency which could check for the presence of flash messages?

You can try to disable the cache, if a flash message is available. For example you could use the duration parameter inside your filters() method (if that’s where you defined the output cache):




'duration' => Yii::app()->user->hasFlash('your-key') ? 0 : $yourCacheTime,



Thanks that did the trick