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 @deprecated This module is only used with driver versions 2.13 and lower.

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 271
def each
  if system_collection? || !QueryCache.enabled? || (respond_to?(:write?, true) && write?)
    super
  else
    @cursor = nil
    unless @cursor = cached_cursor
      session = client.send(:get_session, @options)
      read_with_retry(session, server_selector) do |server|
        result = send_initial_query(server, session)
        if result.cursor_id == 0 || result.cursor_id.nil?
          @cursor = CachedCursor.new(view, result, server, session: session)
          QueryCache.cache_table[cache_key] = @cursor
        else
          @cursor = Mongo::Cursor.new(view, result, server, session: session)
        end
      end
    end
    if block_given?
      if limit
        @cursor.to_a[0...limit].each do |doc|
          yield doc
        end
      else
        @cursor.each do |doc|
          yield doc
        end
      end
    else
      @cursor.to_enum
    end
  end
end

Private Instance Methods

cache_key() click to toggle source
# File lib/mongoid/query_cache.rb, line 314
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 306
def cached_cursor
  if limit
    key = [ collection.namespace, selector, nil, skip, sort, projection, collation  ]
    cursor = QueryCache.cache_table[key]
  end
  cursor || QueryCache.cache_table[cache_key]
end
system_collection?() click to toggle source
# File lib/mongoid/query_cache.rb, line 318
def system_collection?
  collection.name.start_with?('system.')
end