2009年7月10日 星期五

action cache, cahce_path, to_proc

Action Caching
Symbol#to_proc


ActionController::Caching::Actions


Action caching 內部是用 fragment caching 和 around filter 來實做
Action caching internally uses the fragment caching and an around filter to do the job. The fragment cache is named according to both the current host and the path. So a page that is accessed at http://david.somewhere.com/lists/show/1 will result in a fragment named "david.somewhere.com/lists/show/1". This allows the cacher to differentiate between "david.somewhere.com/lists/" and "jamis.somewhere.com/lists/" — which is a helpful way of assisting the subdomain-as-account-key pattern.

不同的url 就是不同的request, cache 也是不同的
Different representations of the same resource, e.g. http://david.somewhere.com/lists and http://david.somewhere.com/lists.xml are treated like separate requests and so are cached separately. Keep in mind when expiring an action cache that :action => 'lists' is not the same as :action => 'list', :format => :xml.

可以讓你自己控制cache_path, 就算是相同的request 也可以利用block 做些判斷生出不同的path, 當你用了block 你可以利用他拿到 controller instance
You can set modify the default action cache path by passing a :cache_path option. This will be passed directly to ActionCachePath.path_for. This is handy for actions with multiple possible routes that should be cached differently. If a block is given, it is called with the current controller instance.

也有 if, unless 來讓你丟block 進去做判斷
And you can also use :if (or :unless) to pass a Proc that specifies when the action should be cached.

如果你把layout 設成false, 你可以只存content, 你可以利用這樣來動態改變layout
If you pass :layout => false, it will only cache your action content. It is useful when your layout has dynamic information.

沒有留言: