Yii Framework Forum: [Solved] Coutputcache: Prevent Flash Message From Getting Cached? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   CodeButterfly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 30-August 12

Posted 13 November 2012 - 12:01 AM

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

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 November 2012 - 02:21 AM

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

http://www.yiiframew...caching.dynamic
1

#3 User is offline   CodeButterfly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 30-August 12

Posted 14 November 2012 - 02:03 PM

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

#4 User is offline   CodeButterfly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 30-August 12

Posted 14 November 2012 - 04:06 PM

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

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 November 2012 - 02:43 AM

View PostCodeButterfly, on 14 November 2012 - 02:03 PM, said:

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.


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,

1

#6 User is offline   CodeButterfly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 30-August 12

Posted 16 November 2012 - 02:59 PM

Thanks that did the trick
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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