module Mongoid::QueryCache::View
Contains enhancements to the Mongo::Collection::View in order to get a cached cursor or a regular cursor on iteration.
@since 5.0.0
Public Instance Methods
each() { |doc| ... }
click to toggle source
Override the default enumeration to handle if the cursor can be cached or not.
@example Iterate over the view.
view.each do |doc| # ... end
@since 5.0.0
Calls superclass method
# File lib/mongoid/query_cache.rb, line 225 def each if system_collection? || !QueryCache.enabled? super else unless cursor = cached_cursor read_with_retry do server = server_selector.select_server(cluster) cursor = CachedCursor.new(view, send_initial_query(server), server) QueryCache.cache_table[cache_key] = cursor end end cursor.each do |doc| yield doc end if block_given? cursor end end
Private Instance Methods
cache_key()
click to toggle source
# File lib/mongoid/query_cache.rb, line 256 def cache_key [ collection.namespace, selector, limit, skip, sort, projection, collation ] end
cached_cursor()
click to toggle source
# File lib/mongoid/query_cache.rb, line 245 def cached_cursor if limit key = [ collection.namespace, selector, nil, skip, sort, projection, collation ] cursor = QueryCache.cache_table[key] if cursor cursor.to_a[0...limit.abs] end end cursor || QueryCache.cache_table[cache_key] end
system_collection?()
click to toggle source
# File lib/mongoid/query_cache.rb, line 260 def system_collection? collection.namespace =~ /^system./ end