Module: Mongoid::QueryCache::View

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

Overview

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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/mongoid/query_cache.rb', line 205

def each
  if system_collection? || !QueryCache.enabled?
    super
  else
    key = cache_key
    cursor = QueryCache.cache_table[key]
    unless cursor
      server = read.select_server(cluster)
      cursor = CachedCursor.new(view, send_initial_query(server), server)
      QueryCache.cache_table[key] = cursor
    end
    cursor.each do |doc|
      yield doc
    end if block_given?
    cursor
  end
end