diff --git a/lib/record_cache/datastore/active_record_30.rb b/lib/record_cache/datastore/active_record_30.rb index e8b9b47..1557bff 100644 --- a/lib/record_cache/datastore/active_record_30.rb +++ b/lib/record_cache/datastore/active_record_30.rb @@ -42,8 +42,9 @@ def find_by_sql_with_record_cache(sql) def try_record_cache(sql, arel) query = arel ? RecordCache::Arel::QueryVisitor.new.accept(arel.ast) : nil - cacheable = query && record_cache.cacheable?(query) - cacheable ? record_cache.fetch(query) : connection.send(:select, sql, "#{name} Load") + record_cache.fetch(query) do + connection.send(:select, sql, "#{name} Load") + end end end diff --git a/lib/record_cache/datastore/active_record_31.rb b/lib/record_cache/datastore/active_record_31.rb index 1a98a07..fa3956e 100644 --- a/lib/record_cache/datastore/active_record_31.rb +++ b/lib/record_cache/datastore/active_record_31.rb @@ -44,10 +44,7 @@ def find_by_sql_with_record_cache(sql, binds = []) def try_record_cache(arel, sql, binds) query = arel ? RecordCache::Arel::QueryVisitor.new(binds).accept(arel.ast) : nil - cacheable = query && record_cache.cacheable?(query) - if cacheable - record_cache.fetch(query) - else + record_cache.fetch(query) do sql = connection.visitor.accept(sql.ast) if sql.respond_to?(:ast) connection.send(:select, sql, "#{name} Load") end diff --git a/lib/record_cache/dispatcher.rb b/lib/record_cache/dispatcher.rb index d98cf04..76a5d01 100644 --- a/lib/record_cache/dispatcher.rb +++ b/lib/record_cache/dispatcher.rb @@ -39,15 +39,10 @@ def [](attribute) @strategy_by_attribute[attribute] end - # Can the cache retrieve the records based on this query? - def cacheable?(query) - !!first_cacheable_strategy(query) - end - # retrieve the record(s) based on the given query (check with cacheable?(query) first) - def fetch(query) - # fetch the results using the first strategy that accepts this query - fetch_from_first_cacheable_strategy(query) + def fetch(query, &block) + strategy = query && ordered_strategies.detect { |strategy| strategy.cacheable?(query) } + strategy ? strategy.fetch(query) : yield end # Update the version store and the record store (used by callbacks) @@ -79,16 +74,6 @@ def record_store(store) RecordCache::MultiRead.test(store) end - # Retrieve the data from the first strategy that can handle the query. - def fetch_from_first_cacheable_strategy(query) - first_cacheable_strategy(query).fetch(query) - end - - # Find the first strategy that can handle this query. - def first_cacheable_strategy(query) - ordered_strategies.detect { |strategy| strategy.cacheable?(query) } - end - # Retrieve all strategies ordered by the fastest strategy first (currently :id, :unique, :index) def ordered_strategies @ordered_strategies ||= begin diff --git a/spec/lib/dispatcher_spec.rb b/spec/lib/dispatcher_spec.rb index 299dabc..968894e 100644 --- a/spec/lib/dispatcher_spec.rb +++ b/spec/lib/dispatcher_spec.rb @@ -30,19 +30,6 @@ @apple_dispatcher[:unknown].should == nil end - it "should return cacheable? true if there is a cacheable strategy that accepts the query" do - query = RecordCache::Query.new - mock(@apple_dispatcher).first_cacheable_strategy(query) { Object.new } - @apple_dispatcher.cacheable?(query).should == true - end - - it "should delegate fetch to the first cacheable strategy" do - query = RecordCache::Query.new - banana_dispatcher = Banana.record_cache - mock(banana_dispatcher).first_cacheable_strategy(query) { mock(Object.new).fetch(query) } - banana_dispatcher.fetch(query) - end - context "record_change" do it "should dispatch record_change to all strategies" do apple = Apple.first