Module: Mongoid::QueryCache::View Deprecated

Extended by:
ActiveSupport::Concern
Defined in:
build/mongoid-7.3/lib/mongoid/query_cache.rb

Overview

Deprecated.

This module is only used with driver versions 2.13 and lower.

Contains enhancements to the Mongo::Collection::View in order to get a cached cursor or a regular cursor on iteration.

Since:

  • 5.0.0

Instance Method Summary collapse

Instance Method Details

#eachObject

Override the default enumeration to handle if the cursor can be cached or not.

Examples:

Iterate over the view.

view.each do |doc|
  # ...
end

Since:

  • 5.0.0



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'build/mongoid-7.3/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