Changeset 8414 for branches/2-1-caching/actionpack
- Timestamp:
- 12/16/07 00:12:27 (1 year ago)
- Files:
-
- branches/2-1-caching/actionpack/lib/action_controller/caching.rb (modified) (1 diff)
- branches/2-1-caching/actionpack/lib/action_controller/caching/actions.rb (modified) (1 diff)
- branches/2-1-caching/actionpack/lib/action_controller/caching/fragments.rb (modified) (1 diff)
- branches/2-1-caching/actionpack/lib/action_controller/caching/pages.rb (modified) (1 diff)
- branches/2-1-caching/actionpack/lib/action_controller/caching/sql_cache.rb (modified) (1 diff)
- branches/2-1-caching/actionpack/lib/action_view/helpers/cache_helper.rb (modified) (1 diff)
- branches/2-1-caching/actionpack/test/controller/fragment_store_setting_test.rb (deleted)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/2-1-caching/actionpack/lib/action_controller/caching.rb
r8393 r8414 53 53 end 54 54 55 # Convenience accessor 56 def cache(key, options = nil, &block) 57 if cache_configured? 58 cache_store.fetch(key, options, &block) 59 else 60 yield 55 protected 56 # Convenience accessor 57 def cache(key, options = nil, &block) 58 if cache_configured? 59 cache_store.fetch(key.to_param, options, &block) 60 else 61 yield 62 end 61 63 end 62 end63 64 64 def cache_configured? 65 self.class.cache_configured? 66 end 65 66 private 67 def cache_configured? 68 self.class.cache_configured? 69 end 67 70 end 68 71 end branches/2-1-caching/actionpack/lib/action_controller/caching/actions.rb
r8393 r8414 55 55 end 56 56 57 def protected_instance_variables_with_action_caching 58 protected_instance_variables_without_action_caching + %w(@action_cache_path) 59 end 57 protected 58 def protected_instance_variables_with_action_caching 59 protected_instance_variables_without_action_caching + %w(@action_cache_path) 60 end 60 61 61 def expire_action(options = {})62 return unless cache_configured?62 def expire_action(options = {}) 63 return unless cache_configured? 63 64 64 if options[:action].is_a?(Array) 65 options[:action].dup.each do |action| 66 expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }))) 65 if options[:action].is_a?(Array) 66 options[:action].dup.each do |action| 67 expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }))) 68 end 69 else 70 expire_fragment(ActionCachePath.path_for(self, options)) 67 71 end 68 else69 expire_fragment(ActionCachePath.path_for(self, options))70 72 end 71 end72 73 73 74 class ActionCacheFilter #:nodoc: branches/2-1-caching/actionpack/lib/action_controller/caching/fragments.rb
r8393 r8414 40 40 end 41 41 end 42 43 def fragment_cache_store=(store_option) #:nodoc: 44 ActiveSupport::Deprecation.warn('The fragment_cache_store= method is now use cache_store=') 45 self.cache_store = store_option 46 end 47 48 def fragment_cache_store #:nodoc: 49 ActiveSupport::Deprecation.warn('The fragment_cache_store method is now use cache_store') 50 cache_store 51 end 42 52 end 43 53 end 44 54 45 # Given a name (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading, 46 # writing, or expiring a cached fragment. If the name is a hash, the generated name is the return 47 # value of url_for on that hash (without the protocol). 48 def fragment_cache_key(name) 49 name.is_a?(Hash) ? url_for(name).split("://").last : name 50 end 51 52 # Called by CacheHelper#cache 53 def cache_erb_fragment(block, name = {}, options = nil) 54 unless cache_configured? then block.call; return end 55 56 buffer = eval(ActionView::Base.erb_variable, block.binding) 57 58 if cache = read_fragment(name, options) 59 buffer.concat(cache) 60 else 61 pos = buffer.length 62 block.call 63 write_fragment(name, buffer[pos..-1], options) 64 end 65 end 66 67 # Writes <tt>content</tt> to the location signified by <tt>name</tt> (see <tt>expire_fragment</tt> for acceptable formats) 68 def write_fragment(name, content, options = nil) 69 return unless cache_configured? 70 71 key = fragment_cache_key(name) 72 73 self.class.benchmark "Cached fragment: #{key}" do 74 fragment_cache_store.write(key, content, options) 55 protected 56 # Given a key (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading, 57 # writing, or expiring a cached fragment. If the key is a hash, the generated key is the return 58 # value of url_for on that hash (without the protocol). 59 def fragment_cache_key(key) 60 key.is_a?(Hash) ? url_for(key).split("://").last : key.to_param 75 61 end 76 62 77 content 78 end 63 # Called by CacheHelper#cache 64 def cache_erb_fragment(block, key = {}, options = nil) 65 unless cache_configured? then block.call; return end 79 66 80 # Reads a cached fragment from the location signified by <tt>name</tt> (see <tt>expire_fragment</tt> for acceptable formats) 81 def read_fragment(name, options = nil) 82 return unless cache_configured? 67 buffer = eval(ActionView::Base.erb_variable, block.binding) 83 68 84 key = fragment_cache_key(name) 85 86 self.class.benchmark "Fragment read: #{key}" do 87 fragment_cache_store.read(key, options) 88 end 89 end 90 91 # Name can take one of three forms: 92 # * String: This would normally take the form of a path like "pages/45/notes" 93 # * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 } 94 # * Regexp: Will destroy all the matched fragments, example: 95 # %r{pages/\d*/notes} 96 # Ensure you do not specify start and finish in the regex (^$) because 97 # the actual filename matched looks like ./cache/filename/path.cache 98 # Regexp expiration is only supported on caches that can iterate over 99 # all keys (unlike memcached). 100 def expire_fragment(name, options = nil) 101 return unless cache_configured? 102 103 key = fragment_cache_key(name) 104 105 if key.is_a?(Regexp) 106 self.class.benchmark "Expired fragments matching: #{key.source}" do 107 fragment_cache_store.delete_matched(key, options) 108 end 109 else 110 self.class.benchmark "Expired fragment: #{key}" do 111 fragment_cache_store.delete(key, options) 69 if cache = read_fragment(key, options) 70 buffer.concat(cache) 71 else 72 pos = buffer.length 73 block.call 74 write_fragment(key, buffer[pos..-1], options) 112 75 end 113 76 end 114 end 77 78 # Writes <tt>content</tt> to the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats) 79 def write_fragment(key, content, options = nil) 80 return unless cache_configured? 81 82 key = fragment_cache_key(key) 83 84 self.class.benchmark "Cached fragment: #{key}" do 85 cache_store.write(key, content, options) 86 end 87 88 content 89 end 90 91 # Reads a cached fragment from the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats) 92 def read_fragment(key, options = nil) 93 return unless cache_configured? 94 95 key = fragment_cache_key(key) 96 97 self.class.benchmark "Fragment read: #{key}" do 98 cache_store.read(key, options) 99 end 100 end 101 102 # Name can take one of three forms: 103 # * String: This would normally take the form of a path like "pages/45/notes" 104 # * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 } 105 # * Regexp: Will destroy all the matched fragments, example: 106 # %r{pages/\d*/notes} 107 # Ensure you do not specify start and finish in the regex (^$) because 108 # the actual filename matched looks like ./cache/filename/path.cache 109 # Regexp expiration is only supported on caches that can iterate over 110 # all keys (unlike memcached). 111 def expire_fragment(key, options = nil) 112 return unless cache_configured? 113 114 key = fragment_cache_key(key) 115 116 if key.is_a?(Regexp) 117 self.class.benchmark "Expired fragments matching: #{key.source}" do 118 cache_store.delete_matched(key, options) 119 end 120 else 121 self.class.benchmark "Expired fragment: #{key}" do 122 cache_store.delete(key, options) 123 end 124 end 125 end 115 126 end 116 127 end branches/2-1-caching/actionpack/lib/action_controller/caching/pages.rb
r8393 r8414 97 97 end 98 98 99 # Expires the page that was cached with the +options+ as a key. Example: 100 # expire_page :controller => "lists", :action => "show" 101 def expire_page(options = {}) 102 return unless perform_caching 99 protected 100 # Expires the page that was cached with the +options+ as a key. Example: 101 # expire_page :controller => "lists", :action => "show" 102 def expire_page(options = {}) 103 return unless perform_caching 103 104 104 if options.is_a?(Hash) 105 if options[:action].is_a?(Array) 106 options[:action].dup.each do |action| 107 self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action))) 105 if options.is_a?(Hash) 106 if options[:action].is_a?(Array) 107 options[:action].dup.each do |action| 108 self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action))) 109 end 110 else 111 self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true))) 108 112 end 109 113 else 110 self.class.expire_page( url_for(options.merge(:only_path => true, :skip_relative_url_root => true)))114 self.class.expire_page(options) 111 115 end 112 else113 self.class.expire_page(options)114 end115 end116 117 # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used118 # If no options are provided, the requested url is used. Example:119 # cache_page "I'm the cached content", :controller => "lists", :action => "show"120 def cache_page(content = nil, options = nil)121 return unless perform_caching && caching_allowed122 123 path = case options124 when Hash125 url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))126 when String127 options128 else129 request.path130 116 end 131 117 132 self.class.cache_page(content || response.body, path) 133 end 118 # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used 119 # If no options are provided, the requested url is used. Example: 120 # cache_page "I'm the cached content", :controller => "lists", :action => "show" 121 def cache_page(content = nil, options = nil) 122 return unless perform_caching && caching_allowed 123 124 path = case options 125 when Hash 126 url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format])) 127 when String 128 options 129 else 130 request.path 131 end 132 133 self.class.cache_page(content || response.body, path) 134 end 134 135 135 136 private branches/2-1-caching/actionpack/lib/action_controller/caching/sql_cache.rb
r8393 r8414 8 8 end 9 9 10 def perform_action_with_caching 11 ActiveRecord::Base.cache do 12 perform_action_without_caching 10 protected 11 def perform_action_with_caching 12 ActiveRecord::Base.cache do 13 perform_action_without_caching 14 end 13 15 end 14 end15 16 end 16 17 end branches/2-1-caching/actionpack/lib/action_view/helpers/cache_helper.rb
r8393 r8414 33 33 # <% end %> 34 34 def cache(name = {}, options = nil, &block) 35 @controller. cache_erb_fragment(block, name, options)35 @controller.send!(:cache_erb_fragment, block, name, options) 36 36 end 37 37 end