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
@return [ Array <BSON::Document> ] The cursor’s cached documents. @api private
Public Instance Methods
Source
# File lib/mongo/caching_cursor.rb, line 34 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
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
Source
Source
# File lib/mongo/caching_cursor.rb, line 66 def try_next @cached_docs ||= [] document = super @cached_docs << document if document document end
Acquires the next document for cursor iteration and then inserts that document in the @cached_docs array.
@api private
Calls superclass method