class Mongo::CachingCursor

A Cursor that attempts to load documents from memory first before hitting the database if the same query has already been executed.

@api semiprivate

Attributes

cached_docs[R]

@return [ Array <BSON::Document> ] The cursor's cached documents. @api private

Public Instance Methods

each() { |doc| ... } click to toggle source

We iterate over the cached documents if they exist already in the cursor otherwise proceed as normal.

@example Iterate over the documents.

cursor.each do |doc|
  # ...
end
Calls superclass method
# File lib/mongo/caching_cursor.rb, line 36
def each
  if @cached_docs
    @cached_docs.each do |doc|
      yield doc
    end

    unless closed?
      # StopIteration raised by try_next ends this loop.
      loop do
        document = try_next
        yield document if document
      end
    end
  else
    super
  end
end
inspect() click to toggle source

Get a human-readable string representation of Cursor.

@example Inspect the cursor.

cursor.inspect

@return [ String ] A string representation of a Cursor instance.

# File lib/mongo/caching_cursor.rb, line 60
def inspect
  "#<Mongo::CachingCursor:0x#{object_id} @view=#{@view.inspect}>"
end
try_next() click to toggle source

Acquires the next document for cursor iteration and then inserts that document in the @cached_docs array.

@api private

Calls superclass method
# File lib/mongo/caching_cursor.rb, line 68
def try_next
  @cached_docs ||= []
  document = super
  @cached_docs << document if document

  document
end