Class: Perforated::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/perforated/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enumerable, strategy = Perforated::Strategy) ⇒ Cache

Returns a new instance of Cache.



9
10
11
12
# File 'lib/perforated/cache.rb', line 9

def initialize(enumerable, strategy = Perforated::Strategy)
  @enumerable = enumerable
  @strategy   = strategy
end

Instance Attribute Details

#enumerableObject

Returns the value of attribute enumerable.



7
8
9
# File 'lib/perforated/cache.rb', line 7

def enumerable
  @enumerable
end

#strategyObject

Returns the value of attribute strategy.



7
8
9
# File 'lib/perforated/cache.rb', line 7

def strategy
  @strategy
end

Instance Method Details

#to_json(rooted: false, batch_size: 1000, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/perforated/cache.rb', line 14

def to_json(rooted: false, batch_size: 1000, &block)
  results = []

  enumerable.each_slice(batch_size) do |subset|
    keyed = key_mapped(subset)

    results << fetch_multi(keyed) do |key|
      if block_given?
        (yield keyed[key]).to_json
      else
        keyed[key].to_json
      end
    end.values
  end

  Perforated::Rebuilder.new(results).rebuild(rooted: rooted)
end