From dd26c2f9f757544f97bb029cf1c7e2b4b631ac2d Mon Sep 17 00:00:00 2001 From: Ben Chiu Date: Tue, 27 May 2008 20:52:31 +0800 Subject: [PATCH] Support querystring params in merb-cache * see README * see CHANGELOG --- merb-cache/README | 29 ++++++++ merb-cache/Rakefile | 7 +- merb-cache/lib/merb-cache/cache-action.rb | 81 +++++++++++++++++------ merb-cache/lib/merb-cache/cache-page.rb | 79 +++++++++++++++++----- merb-cache/lib/merb-cache/merb-cache.rb | 102 ++++++++++++++++++++++++++++- merb-cache/spec/controller.rb | 73 ++++++++++++++++++++- merb-cache/spec/merb-cache-action_spec.rb | 28 ++++---- merb-cache/spec/merb-cache-page_spec.rb | 4 +- merb-cache/spec/merb-cache_spec.rb | 3 + 9 files changed, 346 insertions(+), 60 deletions(-) diff --git a/merb-cache/README b/merb-cache/README index 278c8ee..c88ce99 100644 --- a/merb-cache/README +++ b/merb-cache/README @@ -29,9 +29,13 @@ Implemented cache stores: === Merb::Controller class methods cache_action(action, expiration) + cache_action(action, expiration, options) cache_actions(action, [action, expiration], ...) + cache_actions(action, [action, expiration], [action, expiration, options], ...) cache_page(action, expiration) + cache_page(action, expiration, options) cache_pages(action, [action, expiration], ...) + cache_pages(action, [action, expiration], [action, expiration, options], ...) === Merb::Controller instance methods expire_page(key) @@ -93,6 +97,14 @@ Implemented cache stores: #:store => "memory", # store could be: file, memcache, memory, database, dummy, ... + + # can be nil|:snake|:tree|:hash|:query or a custom string + # such as ":paramname1/:paramname2_and_:paramname3" + #:cache_key_format => nil, + + # expiration time in minutes + #:cache_action_ttl => 10, + #:cache_page_ttl => 10 } @@ -114,6 +126,17 @@ Implemented cache stores: # for multiple action caching: # cache_actions :action_name, [:another_action, 5], :some_action + cache_action :action_with_config + # this will cache the action using the config settings: + # :cache_action_ttl => 10 + # :cache_key_format => nil|:snake|:tree|:query|:hash or custom + + cache_action :action_with_params, 10, :format => :snake, :params => [:id, :name] + # config settings may be overriden per action + # this will cache the action using a snake case key + # key will only include only the params :id and :name + # this cache entry will expire in 10 minutes + def list unless @users = cache_get("active_users") @users = User.all(:active => true) @@ -145,6 +168,12 @@ Implemented cache stores: def index render end + + cache_actions :all, 10, :exclude => [:list] + # this will cache all actions in the current controller + # except for the :list action + # cache entries will expire in 10 minutes + # note: this line must appear at the end of the class declaration end diff --git a/merb-cache/Rakefile b/merb-cache/Rakefile index a66919c..073c769 100644 --- a/merb-cache/Rakefile +++ b/merb-cache/Rakefile @@ -2,7 +2,6 @@ require 'rubygems' require "rake/rdoctask" require 'rake/gempackagetask' require "spec/rake/spectask" -require '../merb_rake_helper' PLUGIN = "merb-cache" NAME = "merb-cache" @@ -65,16 +64,18 @@ Rake::GemPackageTask.new(spec) do |pkg| end install_home = ENV['GEM_HOME'] ? "-i #{ENV['GEM_HOME']}" : "" +windows = (PLATFORM =~ /win32|cygwin/) rescue nil +SUDO = windows ? "" : "sudo" task :install => [:package] do - sh %{#{sudo} gem install #{install_home} pkg/#{NAME}-#{MERB_CACHE_VERSION} --no-update-sources} + sh %{#{SUDO} gem install #{install_home} pkg/#{NAME}-#{MERB_CACHE_VERSION} --no-update-sources} end namespace :jruby do desc "Run :package and install the resulting .gem with jruby" task :install => :package do - sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri} + sh %{#{SUDO} jruby -S gem install #{install_home} pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri} end end diff --git a/merb-cache/lib/merb-cache/cache-action.rb b/merb-cache/lib/merb-cache/cache-action.rb index 0246cc8..5bf08fa 100644 --- a/merb-cache/lib/merb-cache/cache-action.rb +++ b/merb-cache/lib/merb-cache/cache-action.rb @@ -12,32 +12,65 @@ module Merb::Cache::ControllerClassMethods # action:: The name of the action to register # from_now<~minutes>:: # The number of minutes (from now) the cache should persist + # options:: + # Key formats :format => :snake|:tree|:hash|:query or nil for default + # Custom format :format => ":param1/:param2-and_:param3" + # Params :params => [params to include] or false to disable # # ==== Examples # cache_action :mostly_static # cache_action :barely_dynamic, 10 + # cache_action :barely_dynamic, 10, {:format => :hash, :params => [:id, :name]} + # cache_action :barely_dynamic, :format => ":param1/:param2-and_:param3[key]" def cache_action(action, from_now = nil, opts = {}) - cache_actions([action, from_now, opts]) + from_now, opts = nil, from_now if Hash === from_now + + cache_opts = {:format => opts[:format], :params => opts[:params]} + opts.delete(:format); opts.delete(:params); opts.delete(:exclude) + + before("cache_#{action}_before", opts.merge(:only => action, :with => [cache_opts])) + after("cache_#{action}_after", opts.merge(:only => action, :with => [cache_opts])) + alias_method "cache_#{action}_before", :cache_action_before + alias_method "cache_#{action}_after", :cache_action_after + + _actions = Merb::Cache.cached_actions[controller_name] ||= {} + _actions[action] = from_now end # Register actions for action caching (before and after filters) # # ==== Parameter - # actions:: See #cache_action + # actions:: See #cache_action # # ==== Example - # cache_actions :mostly_static, [:barely_dynamic, 10] + # cache_actions :mostly_static, :barely_dynamic + # cache_actions :mostly_static, [:barely_dynamic, 10] + # cache_actions :barely_dynamic, [:barely_dynamic, 10, :format => :hash] + # cache_actions :barely_dynamic, [:barely_dynamic, 10, :params => [:id, :name]] + # cache_actions :all, 10, :exclude => [:show], :format => :snake, :params => [:id, :name] def cache_actions(*actions) - actions.each do |action, from_now, opts| - from_now, opts = nil, from_now if Hash === from_now - - before("cache_#{action}_before", opts.merge(:only => action)) - after("cache_#{action}_after", opts.merge(:only => action)) - alias_method "cache_#{action}_before", :cache_action_before - alias_method "cache_#{action}_after", :cache_action_after - - _actions = Merb::Cache.cached_actions[controller_name] ||= {} - _actions[action] = from_now + config = Merb::Plugins.config[:merb_cache] + + if actions[0] == :all + from_now = Hash === actions[1] ? config[:cache_action_ttl] : actions[1] + opts = Hash === actions[1] ? actions[1] : actions[2] || {} + excludes = opts[:exclude] || [] + actions = self.instance_methods(false).map {|action| + [action.to_sym, from_now, opts] unless excludes.include?(action.to_sym) + }.compact + end + + actions.each do |act_opts| + if Array === act_opts + action = act_opts[0] + from_now = Hash === act_opts[1] ? config[:cache_action_ttl] : act_opts[1] + opts = Hash === act_opts[1] ? act_opts[1] : act_opts[2] + else + action = act_opts + from_now = config[:cache_action_ttl] + opts = {} + end + cache_action(action, from_now, opts||{}) end true end @@ -100,19 +133,27 @@ module Merb::Cache::ControllerInstanceMethods # If request.path is "/", the key will be "index" # If request.path is "/news/show/1", the key will be "/news/show/1" # If request.path is "/news/show/", the key will be "/news/show" - def _cache_action(data = nil) + def _cache_action(data = nil, opts = {}) controller = controller_name action = action_name.to_sym actions = Merb::Controller._cache.cached_actions[controller] return unless actions && actions.key?(action) + path = request.path.chomp("/") path = "index" if path.empty? + + _format = opts[:format] || Merb::Controller._cache.config[:cache_key_format] + _params = opts[:params]==false ? nil : Merb::Request.query_parse(request.query_string, '&', true) + _params.delete_if {|k,v| !opts[:params].include?(k.to_sym) } if _params && opts[:params] + + key = Merb::Controller._cache.key_for({:key => path, :params => _params, :format => _format}) + if data from_now = Merb::Controller._cache.cached_actions[controller][action] - Merb::Controller._cache.store.cache_set(path, data, from_now) + Merb::Controller._cache.store.cache_set(key, data, from_now) else @capture_action = false - _data = Merb::Controller._cache.store.cache_get(path) + _data = Merb::Controller._cache.store.cache_get(key) throw(:halt, _data) unless _data.nil? @capture_action = true end @@ -120,16 +161,16 @@ module Merb::Cache::ControllerInstanceMethods end # before filter - def cache_action_before + def cache_action_before(opts) # recalls a cached entry or set @capture_action to true in order # to grab the response in the after filter - _cache_action + _cache_action(nil, opts) end # after filter - def cache_action_after + def cache_action_after(opts) # takes the body of the response and put it in cache # if the cache entry expired, if it doesn't exist or status is 200 - _cache_action(body) if @capture_action && status == 200 + _cache_action(body, opts) if @capture_action && status == 200 end end diff --git a/merb-cache/lib/merb-cache/cache-page.rb b/merb-cache/lib/merb-cache/cache-page.rb index f216d25..af6e75a 100644 --- a/merb-cache/lib/merb-cache/cache-page.rb +++ b/merb-cache/lib/merb-cache/cache-page.rb @@ -13,29 +13,65 @@ module Merb::Cache::ControllerClassMethods # action:: The name of the action to register # from_now<~minutes>:: # The number of minutes (from now) the cache should persist + # options:: + # Key formats :format => :snake|:tree|:hash|:query or nil for default + # Custom format :format => ":param1/:param2-and_:param3" + # Params :params => [params to include] or false to disable # # ==== Examples # cache_page :mostly_static # cache_page :barely_dynamic, 10 - def cache_page(action, from_now = nil) - cache_pages([action, from_now]) + # cache_page :barely_dynamic, 10, {:format => :hash, :params => [:id, :name]} + # cache_page :barely_dynamic, :format => ":param1/:param2-and_:param3[key]" + def cache_page(action, from_now = nil, opts = {}) + from_now, opts = nil, from_now if Hash === from_now + + cache_opts = {:format => opts[:format], :params => opts[:params]} + opts.delete(:format); opts.delete(:params); opts.delete(:exclude) + + before("cache_#{action}_before", opts.merge(:only => action, :with => [cache_opts])) + after("cache_#{action}_after", opts.merge(:only => action, :with => [cache_opts])) + alias_method "cache_#{action}_before", :cache_page_before + alias_method "cache_#{action}_after", :cache_page_after + + _pages = Merb::Cache.cached_pages[controller_name] ||= {} + _pages[action] = [from_now, 0] end # Register actions for page caching (before and after filters) # # ==== Parameter - # pages:: See #cache_page + # actions:: See #cache_page # # ==== Example - # cache_pages :mostly_static, [:barely_dynamic, 10] + # cache_pages :mostly_static, :barely_dynamic + # cache_pages :mostly_static, [:barely_dynamic, 10] + # cache_pages :barely_dynamic, [:barely_dynamic, 10, :format => :hash] + # cache_pages :barely_dynamic, [:barely_dynamic, 10, :params => [:id, :name]] + # cache_pages :all, 10, :exclude => [:show], :format => :snake, :params => [:id, :name] def cache_pages(*pages) - if pages.any? && !Merb::Cache.cached_pages.key?(controller_name) - before(:cache_page_before) - after(:cache_page_after) + config = Merb::Plugins.config[:merb_cache] + + if pages[0] == :all + from_now = Hash === pages[1] ? config[:cache_page_ttl] : pages[1] + opts = Hash === pages[1] ? pages[1] : pages[2] || {} + excludes = opts[:exclude] || [] + pages = self.instance_methods(false).map {|action| + [action.to_sym, from_now, opts] unless excludes.include?(action.to_sym) + }.compact end - pages.each do |action, from_now| - _pages = Merb::Cache.cached_pages[controller_name] ||= {} - _pages[action] = [from_now, 0] + + pages.each do |page_opts| + if Array === page_opts + action = page_opts[0] + from_now = Hash === page_opts[1] ? config[:cache_page_ttl] : page_opts[1] + opts = Hash === page_opts[1] ? page_opts[1] : page_opts[2] + else + action = page_opts + from_now = config[:cache_page_ttl] + opts = {} + end + cache_page(action, from_now, opts||{}) end true end @@ -121,18 +157,27 @@ module Merb::Cache::ControllerInstanceMethods # If request.path is "/news/show/1", the name will be "/news/show/1.html" # If request.path is "/news/show/", the name will be "/news/show.html" # If request.path is "/news/styles.css", the name will be "/news/styles.css" - def _cache_page(data = nil) + def _cache_page(data = nil, opts = {}) return if Merb::Controller._cache.config[:disable_page_caching] controller = controller_name action = action_name.to_sym pages = Merb::Controller._cache.cached_pages[controller] return unless pages && pages.key?(action) + path = request.path.chomp("/") path = "index" if path.empty? + + _format = opts[:format] || Merb::Controller._cache.config[:cache_key_format] + _params = opts[:params]==false ? nil : Merb::Request.query_parse(request.query_string, '&', true) + _params.delete_if {|k,v| !opts[:params].include?(k.to_sym) } if _params && opts[:params] + + key = Merb::Controller._cache.key_for({:key => path, :params => _params, :format => _format}) + no_format = params[:format].nil? || params[:format].empty? ext = "." + (no_format ? DEFAULT_PAGE_EXTENSION : params[:format]) - ext = nil if File.extname(path) == ext - cache_file = Merb::Controller._cache.config[:cache_html_directory] / "#{path}#{ext}" + ext = nil if File.extname(key) == ext + cache_file = Merb::Controller._cache.config[:cache_html_directory] / "#{key}#{ext}" + if data cache_directory = File.dirname(cache_file) FileUtils.mkdir_p(cache_directory) @@ -188,16 +233,16 @@ module Merb::Cache::ControllerInstanceMethods end # before filter - def cache_page_before + def cache_page_before(opts) # recalls a cached entry or set @capture_page to true in order # to grab the response in the after filter - _cache_page + _cache_page(nil, opts) end # after filter - def cache_page_after + def cache_page_after(opts) # takes the body of the response # if the cache entry expired, if it doesn't exist or status is 200 - _cache_page(body) if @capture_page && status == 200 + _cache_page(body, opts) if @capture_page && status == 200 end end diff --git a/merb-cache/lib/merb-cache/merb-cache.rb b/merb-cache/lib/merb-cache/merb-cache.rb index 36bf464..705b462 100644 --- a/merb-cache/lib/merb-cache/merb-cache.rb +++ b/merb-cache/lib/merb-cache/merb-cache.rb @@ -1,6 +1,7 @@ require "merb-cache/cache-action" require "merb-cache/cache-page" require "merb-cache/cache-fragment" +require "digest/md5" class Merb::Cache attr_reader :config, :store @@ -30,6 +31,14 @@ class Merb::Cache #:store => "memory", # store could be: file, memcache, memory, database, dummy, ... + + # can be nil|:snake|:tree|:hash|:query or a custom string + # such as ":paramname1/:paramname2_and_:paramname3" + #:cache_key_format => :snake, + + # expiration time in minutes + #:cache_action_ttl => 10, + #:cache_page_ttl => 10 } # Called in the after_app_loads loop and instantiate the right backend @@ -109,6 +118,7 @@ class Merb::Cache # # ==== Options (options) # :key:: The complete or partial key that will be computed. + # :format:: The formatting style that will be used for the key. # :action:: The action name that will be used to compute the key # :controller:: The controller name that will be part of the key # :params:: @@ -117,10 +127,56 @@ class Merb::Cache # ==== Examples # cache_set("my_key", @data) # cache_get(:key => "root_key", :params => [session[:me], params[:id]]) + # cache_get(:key => "root_key", :format => nil|:snake|:hash|:query|:tree) + # cache_get(:key => "root_key", :format => ":one_:two[a]_and_:three[]") # # ==== Returns # The computed key - def key_for(options, controller, controller_based = false) + # def key_for(options, controller = nil, controller_based = false) + # key = "" + # if options.is_a? Hash + # case + # when key = options[:key] + # when action = options[:action] + # controller = options[:controller] || controller + # key = "/#{controller}/#{action}" + # end + # + # _params = options[:params] + # _format = options[:format] + # + # if _params && !_params.empty? + # vals = _params.to_a.map {|p| String === p ? p : (Hash === p[1] ? p[1].values : p[1]) }.flatten + # case _format + # when nil + # _params = _params.to_a.flatten.join('_').gsub('/','%2F') # key1_val1_key2_val2 + # when :snake + # _params = vals.join('_').gsub('/','%2F') # val1_val2 + # when :hash + # _params = Digest::MD5.hexdigest(_params.to_s) # 32-bit md5 hash + # when :query + # _params = Merb::Request.params_to_query_string(_params) # key1=val1&key2=val2 + # when :tree + # _params = vals[0..10].map {|v| v.gsub('/','%2F')}.join('/') # /val1/val2 + # else + # _params.each do |k,v| + # _format.sub!(":#{k}", v.gsub('/','%2F')) if String === v + # _format.sub!(":#{k}[]", v[0].gsub('/','%2F')) if Array === v + # v.each {|kk,vv| _format.sub!(":#{k}[#{kk}]", vv.gsub('/','%2F')) } if Hash === v + # end + # _params = _format + # end + # key += '/' + sanitize_cache_key(_params) unless _params.blank? + # else + # #key += '/index' # all pages in one dir + # end + # else + # key = controller_based ? "/#{controller}/#{options}" : options + # end + # key + # end + # + def key_for(options, controller = nil, controller_based = false) key = "" if options.is_a? Hash case @@ -129,8 +185,34 @@ class Merb::Cache controller = options[:controller] || controller key = "/#{controller}/#{action}" end - if _params = options[:params] - key += "/" + _params.join("/") + + _params = options[:params] + _format = options[:format] + + if _params && !_params.empty? + vals = _params.to_a.map {|p| String === p ? p : (Hash === p[1] ? p[1].values : p[1]) }.flatten + case _format + when nil + _params = _params.to_a.flatten.join('_').gsub('/','%2F') # key1_val1_key2_val2 + when :snake + _params = vals.join('_').gsub('/','%2F') # val1_val2 + when :hash + _params = Digest::MD5.hexdigest(_params.to_s) # 32-bit md5 hash + when :query + _params = Merb::Request.params_to_query_string(_params) # key1=val1&key2=val2 + when :tree + _params = vals[0..10].map {|v| v.gsub('/','%2F')}.join('/') # /val1/val2 + else + _params.each do |k,v| + _format.sub!(":#{k}", v.gsub('/','%2F')) if String === v + _format.sub!(":#{k}[]", v[0].gsub('/','%2F')) if Array === v + v.each {|kk,vv| _format.sub!(":#{k}[#{kk}]", vv.gsub('/','%2F')) } if Hash === v + end + _params = _format + end + key += '/' + sanitize_cache_key(_params) unless _params.blank? + else + #key += '/index' # all pages in one dir end else key = controller_based ? "/#{controller}/#{options}" : options @@ -138,6 +220,20 @@ class Merb::Cache key end + # Removes illegal chars from key and allows cached pages to be safely + # stored on any filesystem. You may override this method in your app + # to match how filenames are escaped by your web server. + # + # ==== Parameters + # key:: The key to sanitize + # + # ==== Returns + # The sanitized key + # + def sanitize_cache_key(key = "") + key.gsub(/[\\\:\*\?\"\<\>\|\s]/,'_') + end + module ControllerInstanceMethods # Mixed in Merb::Controller and provides expire_all for action and fragment caching. def expire_all diff --git a/merb-cache/spec/controller.rb b/merb-cache/spec/controller.rb index c11110c..98ba762 100644 --- a/merb-cache/spec/controller.rb +++ b/merb-cache/spec/controller.rb @@ -1,6 +1,8 @@ class CacheController < Merb::Controller self._template_root = File.dirname(__FILE__) / "views" + before :fix_query_string + cache_action :action3 cache_action :action4, 0.05 # or cache_actions :action3, [:action4, 0.05] @@ -9,12 +11,33 @@ class CacheController < Merb::Controller cache_page :action6, 0.05 # or cache_pages :action5, [:action6, 0.05] cache_page :action7 - + cache_action :action8, 0.05, :if => proc {|controller| !controller.params[:id].empty?} cache_action :action9, 0.05, :unless => proc {|controller| controller.params[:id].empty?} cache_action :action10, :if => :non_empty_id? cache_action :action11, :unless => :empty_id? + cache_action :action12, :params => [:p1, :p2] + cache_action :action13, :format => :snake + cache_action :action14, :format => :tree + cache_action :action15, :format => :hash + cache_action :action16, :format => :query + cache_action :action17, :format => ":p1/:p2_and_:p3" + + cache_page :action18, :params => [:p1, :p2] + cache_page :action19, :format => :snake + cache_page :action20, :format => :tree + cache_page :action21, :format => :hash + cache_page :action22, :format => :query + cache_page :action23, :format => ":p1/:p2_and_:p3" + + def fix_query_string + params[:id] ||= '' + p = Merb::Request.query_parse(request.query_string, '&', true) + p.delete_if {|k,v| [:id,:format].include?(k.to_sym) } + request.env['QUERY_STRING'] = Merb::Request.params_to_query_string(p) + end + def action1 render end @@ -73,6 +96,54 @@ class CacheController < Merb::Controller "test action11" end + def action12 + "test action12" + end + + def action13 + "test action13" + end + + def action14 + "test action14" + end + + def action15 + "test action15" + end + + def action16 + "test action16" + end + + def action17 + "test action17" + end + + def action18 + "test action18" + end + + def action19 + "test action19" + end + + def action20 + "test action20" + end + + def action21 + "test action21" + end + + def action22 + "test action22" + end + + def action23 + "test action23" + end + def empty_id? params[:id].empty? end diff --git a/merb-cache/spec/merb-cache-action_spec.rb b/merb-cache/spec/merb-cache-action_spec.rb index eeefd00..b067721 100644 --- a/merb-cache/spec/merb-cache-action_spec.rb +++ b/merb-cache/spec/merb-cache-action_spec.rb @@ -38,12 +38,12 @@ describe "merb-cache-action" do it "should cache action with full path (action4) and expire in 3 seconds" do CACHE.expire_action :match => true, :action => "action4" - CACHE.cached_action?(:action => "action4", :params => %w(path to nowhere)).should be_false + CACHE.cached_action?(:action => "action4", :params => %w(path to nowhere), :format => :tree).should be_false c = get("/cache_controller/action4/path/to/nowhere/") - c.cached_action?(:action => "action4", :params => %w(path to nowhere)).should be_true + c.cached_action?(:action => "action4", :params => %w(path to nowhere), :format => :tree).should be_true sleep 3.5 c.cache_get("/cache_controller/action4/path/to/nowhere").should be_nil - c.cached_action?(:action => "action4", :params => %w(path to nowhere)).should be_false + c.cached_action?(:action => "action4", :params => %w(path to nowhere), :format => :tree).should be_false end it "should expire action in many ways" do @@ -60,12 +60,12 @@ describe "merb-cache-action" do CACHE.cached_action?(:action => "action4").should be_false c = get("/cache_controller/action4/id1/id2") - CACHE.expire_action(:action => "action4", :params => %w(id1 id2)) - CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false + CACHE.expire_action(:action => "action4", :params => %w(id1 id2), :format => :tree) + CACHE.cached_action?(:action => "action4", :params => %w(id1 id2), :format => :tree).should be_false c = get("/cache_controller/action4/id1/id2") - CACHE.expire_action(:action => "action4", :match => true) - CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false + CACHE.expire_action(:action => "action4", :match => true, :format => :tree) + CACHE.cached_action?(:action => "action4", :params => %w(id1 id2), :format => :tree).should be_false c = get("/cache_controller/action4") CACHE.expire_action(:action => "action4", :controller => "cache_controller") @@ -73,26 +73,26 @@ describe "merb-cache-action" do c = get("/cache_controller/action4/id1/id2") CACHE.expire_action(:action => "action4", :params => %w(id1), :match => true) - CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false + CACHE.cached_action?(:action => "action4", :params => %w(id1 id2), :format => :tree).should be_false c = get("/cache_controller/action4/id1/id2") CACHE.expire_action(:action => "action4", :controller => "cache_controller", :match => true) - CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false + CACHE.cached_action?(:action => "action4", :params => %w(id1 id2), :format => :tree).should be_false c = get("/cache_controller/action4/id1/id2") CACHE.expire_action(:action => "action4", :controller => "cache_controller", :params => %w(id1), :match => true) - CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false + CACHE.cached_action?(:action => "action4", :params => %w(id1 id2), :format => :tree).should be_false c = get("/cache_controller/action4/id1/id2") - CACHE.expire_action(:action => "action4", :controller => "cache_controller", :params => %w(id1 id2)) - CACHE.cached_action?(:action => "action4", :controller => "cache_controller", :params => %w(id1 id2)).should be_false + CACHE.expire_action(:action => "action4", :controller => "cache_controller", :params => %w(id1 id2), :format => :tree) + CACHE.cached_action?(:action => "action4", :controller => "cache_controller", :params => %w(id1 id2), :format => :tree).should be_false c = get("/cache_controller/action4") CACHE.expire_action(:key => "/cache_controller/action4") CACHE.cached_action?(:key => "/cache_controller/action4").should be_false c = get("/cache_controller/action4/id1/id2") - CACHE.expire_action(:key => "/cache_controller/action4", :params => %w(id1 id2)) - CACHE.cached_action?(:key => "/cache_controller/action4", :params => %w(id1 id2)).should be_false + CACHE.expire_action(:key => "/cache_controller/action4", :params => %w(id1 id2), :format => :tree) + CACHE.cached_action?(:key => "/cache_controller/action4", :params => %w(id1 id2), :format => :tree).should be_false c = get("/cache_controller/action4/id1/id2") CACHE.expire_action(:key => "/cache_controller/action4/id1", :match => true) diff --git a/merb-cache/spec/merb-cache-page_spec.rb b/merb-cache/spec/merb-cache-page_spec.rb index b394f78..4863dbe 100644 --- a/merb-cache/spec/merb-cache-page_spec.rb +++ b/merb-cache/spec/merb-cache-page_spec.rb @@ -13,7 +13,7 @@ describe "merb-cache-page" do it "should cache page (action5) with full path" do c = get("/cache_controller/action5/this/is/a/test") - c.cached_page?(:action => "action5", :params => %w(this is a test)).should be_true + c.cached_page?(:action => "action5", :params => %w(this is a test), :format => :tree).should be_true end it "should expire page (action5) with full path" do @@ -43,7 +43,7 @@ describe "merb-cache-page" do c = get("/cache_controller/action6/path/to/nowhere/") now = Time.now.to_s c.body.strip.should == now - c.cached_page?(:action => "action6", :params => %w(path to nowhere)).should be_true + c.cached_page?(:action => "action6", :params => %w(path to nowhere), :format => :tree).should be_true sleep 1 c = get("/cache_controller/action6/path/to/nowhere") c.body.strip.should == now diff --git a/merb-cache/spec/merb-cache_spec.rb b/merb-cache/spec/merb-cache_spec.rb index 81e3bdf..748e692 100644 --- a/merb-cache/spec/merb-cache_spec.rb +++ b/merb-cache/spec/merb-cache_spec.rb @@ -10,6 +10,9 @@ CACHE.expire_all puts "Using #{CACHE._cache.store.cache_store_type.inspect} store" +require File.dirname(__FILE__) + "/merb-cache-keys_spec" require File.dirname(__FILE__) + "/merb-cache-fragment_spec" require File.dirname(__FILE__) + "/merb-cache-action_spec" +require File.dirname(__FILE__) + "/merb-cache-action-params_spec" require File.dirname(__FILE__) + "/merb-cache-page_spec" +require File.dirname(__FILE__) + "/merb-cache-page-params_spec" -- 1.5.5.1015.g9d258