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 220
def each
  if system_collection? || !QueryCache.enabled?
    super
  else
    unless cursor = cached_cursor
      server = read.select_server(cluster)
      cursor = CachedCursor.new(view, send_initial_query(server), server)
      QueryCache.cache_table[cache_key] = cursor
    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 250
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 238
def cached_cursor
  if limit
    key = [ collection.namespace, selector, nil, skip, sort, projection, collation  ]
    cursor = QueryCache.cache_table[key]
    if cursor
      limited_docs = cursor.to_a[0...limit.abs]
      cursor.instance_variable_set(:@cached_documents, limited_docs)
    end
  end
  cursor || QueryCache.cache_table[cache_key]
end
system_collection?() click to toggle source
# File lib/mongoid/query_cache.rb, line 254
def system_collection?
  collection.namespace =~ /^system./
end