Permalink
Browse files

Fix query cache on first/last calls.

[ fix #3514 ]
  • Loading branch information...
durran committed Feb 1, 2014
1 parent 6c1b47f commit 78926750460fd274ff68712fc531ee57d8213798
Showing with 106 additions and 23 deletions.
  1. +2 −0 CHANGELOG.md
  2. +63 −23 lib/mongoid/query_cache.rb
  3. +41 −0 spec/mongoid/query_cache_spec.rb
View
@@ -341,6 +341,8 @@ For instructions on upgrading to newer versions, visit
### Resolved Issues
+* \#3514 Fixed query cache to work on first/last calls.
+
* \#3383/\#3495 Fix has_and_belongs_to_many eager load. (Arthur Neves)
* \#3492 $rename operations should not mongoize values. (Vladislav Melanitskiy)
View
@@ -123,29 +123,6 @@ def #{method_name}_with_clear_cache(*args)
end
end
- module Query
- extend ActiveSupport::Concern
-
- included do
- extend QueryCache::Base
- alias_method_chain :cursor, :cache
- alias_query_cache_clear :remove, :remove_all, :update, :update_all, :upsert
- end
-
- def cursor_with_cache
- CachedCursor.new(session, operation)
- end
- end
-
- module Collection
- extend ActiveSupport::Concern
-
- included do
- extend QueryCache::Base
- alias_query_cache_clear :insert
- end
- end
-
# Module to include in objects which need to wrap caching behaviour around
# them.
#
@@ -170,6 +147,69 @@ def instrument(key, &block)
end
end
+ # Adds behaviour around caching to a Moped Query object.
+ #
+ # @since 4.0.0
+ module Query
+ extend ActiveSupport::Concern
+ include Cacheable
+
+ included do
+ extend QueryCache::Base
+ alias_method_chain :cursor, :cache
+ alias_method_chain :first, :cache
+ alias_query_cache_clear :remove, :remove_all, :update, :update_all, :upsert
+ end
+
+ # Provide a wrapped query cache cursor.
+ #
+ # @example Get the wrapped caching cursor.
+ # query.cursor_with_cache
+ #
+ # @return [ CachedCursor ] The cached cursor.
+ #
+ # @since 4.0.0
+ def cursor_with_cache
+ CachedCursor.new(session, operation)
+ end
+
+ # Override first with caching.
+ #
+ # @example Get the first with a cache.
+ # query.first_with_cache
+ #
+ # @return [ Hash ] The first document.
+ #
+ # @since 4.0.0
+ def first_with_cache
+ with_cache do
+ first_without_cache
+ end
+ end
+
+ private
+
+ def cache_key
+ [ operation.database, operation.collection, operation.selector ]
+ end
+
+ def system_collection?
+ operation.collection =~ /^system./
+ end
+ end
+
+ # Adds behaviour to the query cache for collections.
+ #
+ # @since 4.0.0
+ module Collection
+ extend ActiveSupport::Concern
+
+ included do
+ extend QueryCache::Base
+ alias_query_cache_clear :insert
+ end
+ end
+
# A Cursor that attempts to load documents from memory first before hitting
# the database if the same query has already been executed.
#
@@ -7,6 +7,47 @@
Mongoid::QueryCache.cache { spec.run }
end
+ context "when querying for a single document" do
+
+ [ :first, :one ].each do |method|
+
+ before do
+ Band.all.send(method)
+ end
+
+ context "when query cache disable" do
+
+ before do
+ Mongoid::QueryCache.enabled = false
+ end
+
+ it "queries again" do
+ expect_query(1) do
+ Band.all.send(method)
+ end
+ end
+ end
+
+ context "with same selector" do
+
+ it "does not query again" do
+ expect_no_queries do
+ Band.all.send(method)
+ end
+ end
+ end
+
+ context "with different selector" do
+
+ it "queries again" do
+ expect_query(1) do
+ Band.where(id: 1).send(method)
+ end
+ end
+ end
+ end
+ end
+
context "when querying in the same collection" do
before do

0 comments on commit 7892675

Please sign in to comment.