类:Mongo::CachingCursor

继承:
Cursor
  • 对象
显示全部
定义于:
lib/mongo/caching_cursor.rb

Overview

如果已经执行了相同的查询,则游标会先尝试从内存加载文档,然后再访问数据库。

实例属性摘要折叠

游标继承的属性

#connection #context #initial_result #resume_token # 服务器 #view

实例方法摘要折叠

Cursor继承的方法

#batch_size#close#lost?#collection_namefinalize#completely_iterated?#get_more#id#initialize#kill_spec#refresh_timeout!

Retryable 中包含的方法

#read_worker#select_server#with_overload_retry#write_worker

构造函数详情

该类从Mongo::Cursor继承了一个构造函数

实例属性详细信息

# cached_docs数组 <BSON::Document> (只读)

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回游标的缓存文档。

返回:

  • ( Array <BSON::Document> )

    游标的缓存文档。



25
26
27
# File 'lib/ Mongo/caching_cursor.rb', line 25

def cached_docs
  @cached_docs
end

实例方法详细信息

#each (&block) ⇒对象

如果缓存的文档已存在于游标中,则我们将对其进行迭代,否则将照常进行。

例子:

遍历文档。

cursor.each do |doc|
  # ...
end


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ Mongo/caching_cursor.rb', line 34

def ()
  if @cached_docs
    @cached_docs.()

    除非 已关闭?
      # 由 try_next 引发的 StopIteration 会结束此循环。
      循环 do
        文档 = try_next
        产量 文档 if 文档
      end
    end
  else
    
  end
end

#检查string

获取 Cursor 的人类可读string表示形式。

例子:

检查游标。

cursor.inspect

返回:

  • ( string )

    Cursor 实例的string表示形式。



56
57
58
# File 'lib/ Mongo/caching_cursor.rb', line 56

def 检查
  " #<Mongo::CachingCursor:0 x # { object_id } @view= #{ @view .inspect } } > "
end

# try_next对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

获取游标迭代的下一个文档,然后将该文档插入到 @cached_docs 数组中。



64
65
66
67
68
69
70
# File 'lib/ Mongo/caching_cursor.rb', line 64

def try_next
  @cached_docs ||= []
  文档 = 
  @cached_docs << 文档 if 文档

  文档
end