Ticket #7315: docs_for_fragment_caching.patch
| File docs_for_fragment_caching.patch, 5.5 kB (added by bscofield, 2 years ago) |
|---|
-
caching.rb
old new 216 216 217 217 end 218 218 219 class ActionCachePath 219 class ActionCachePath #:nodoc: 220 220 attr_reader :controller, :options 221 221 222 222 class << self … … 269 269 # <%= render :partial => "topic", :collection => Topic.find(:all) %> 270 270 # <% end %> 271 271 # 272 # This cache will bind to the name of action that called it. So you would be able to invalidate it using 273 # <tt>expire_fragment(:controller => "topics", :action => "list")</tt> -- if that was the controller/action used. This is not too helpful 274 # if you need to cache multiple fragments per action or if the action itself is cached using <tt>caches_action</tt>. So instead we should 275 # qualify the name of the action used with something like: 272 # This cache will bind to the name of the action that called it, so if this code was part of the view for the topics/list action, you would 273 # be able to invalidate it using <tt>expire_fragment(:controller => "topics", :action => "list")</tt>. 274 # 275 # This default behavior is of limited use if you need to cache multiple fragments per action or if the action itself is cached using 276 # <tt>caches_action</tt>, so we also have the option to qualify the name of the cached fragment with something like: 276 277 # 277 278 # <% cache(:action => "list", :action_suffix => "all_topics") do %> 278 279 # 279 # That would result in a name such as "/topics/list/all_topics", which wouldn't conflict with any action cache and neither with another 280 # fragment using a different suffix. Note that the URL doesn't have to really exist or be callable. We're just using the url_for system 281 # to generate unique cache names that we can refer to later for expirations. The expiration call for this example would be 282 # <tt>expire_fragment(:controller => "topics", :action => "list", :action_suffix => "all_topics")</tt>. 280 # That would result in a name such as "/topics/list/all_topics", avoiding conflicts with the action cache and with any fragments that use a 281 # different suffix. Note that the URL doesn't have to really exist or be callable - the url_for system is just used to generate unique 282 # cache names that we can refer to when we need to expire the cache. 283 # 284 # The expiration call for this example is: 285 # 286 # expire_fragment(:controller => "topics", :action => "list", :action_suffix => "all_topics") 283 287 # 284 288 # == Fragment stores 285 289 # 286 # In order to use the fragment caching, you need to designate where the caches should be stored. This is done by assigning a fragment store 287 # of which there are four different kinds: 290 # By default, cached fragments are stored in memory. The available store options are: 288 291 # 289 # * FileStore: Keeps the fragments on disk in the +cache_path+, which works well for all types of environments and shares the fragments for290 # all the web server processes running off the same application directory.292 # * FileStore: Keeps the fragments on disk in the +cache_path+, which works well for all types of environments and allows all 293 # processes running from the same application directory to access the cached content. 291 294 # * MemoryStore: Keeps the fragments in memory, which is fine for WEBrick and for FCGI (if you don't care that each FCGI process holds its 292 295 # own fragment store). It's not suitable for CGI as the process is thrown away at the end of each request. It can potentially also take 293 296 # up a lot of memory since each process keeps all the caches in memory. … … 309 312 @@fragment_cache_store = MemoryStore.new 310 313 cattr_reader :fragment_cache_store 311 314 315 # Defines the storage option for cached fragments 312 316 def self.fragment_cache_store=(store_option) 313 317 store, *parameters = *([ store_option ].flatten) 314 318 @@fragment_cache_store = if store.is_a?(Symbol) … … 322 326 end 323 327 end 324 328 329 # Given a name (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading, 330 # writing, or expiring a cached fragment. If the name is a hash, the generated name is the return 331 # value of url_for on that hash (without the protocol). 325 332 def fragment_cache_key(name) 326 333 name.is_a?(Hash) ? url_for(name).split("://").last : name 327 334 end … … 341 348 end 342 349 end 343 350 351 # Writes <tt>content</tt> to the location signified by <tt>name</tt> (see <tt>expire_fragment</tt> for acceptable formats) 344 352 def write_fragment(name, content, options = nil) 345 353 return unless perform_caching 346 354 … … 351 359 content 352 360 end 353 361 362 # Reads a cached fragment from the location signified by <tt>name</tt> (see <tt>expire_fragment</tt> for acceptable formats) 354 363 def read_fragment(name, options = nil) 355 364 return unless perform_caching 356 365 … … 367 376 # %r{pages/\d*/notes} 368 377 # Ensure you do not specify start and finish in the regex (^$) because 369 378 # the actual filename matched looks like ./cache/filename/path.cache 370 # Regexp expiration is not supported on caches which can'titerate over371 # all keys , such as memcached.379 # Regexp expiration is only supported on caches that can iterate over 380 # all keys (unlike memcached). 372 381 def expire_fragment(name, options = nil) 373 382 return unless perform_caching 374 383