Class: SpreeCmCommissioner::RedisStock::CachedInventoryItemsBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory_items) ⇒ CachedInventoryItemsBuilder

Returns a new instance of CachedInventoryItemsBuilder.



6
7
8
# File 'app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb', line 6

def initialize(inventory_items)
  @inventory_items = inventory_items
end

Instance Attribute Details

#inventory_itemsObject (readonly)

Returns the value of attribute inventory_items.



4
5
6
# File 'app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb', line 4

def inventory_items
  @inventory_items
end

Instance Method Details

#callObject

output: [ CachedInventoryItem(...), CachedInventoryItem(...) ]

Reads BOTH pools in one MGET. Reporting only the pool a caller asked for would mean taking a pool argument here and threading it through every service that warms or reads stock; four numbers in the row we already fetch costs one MGET either way.



15
16
17
18
19
20
21
22
23
# File 'app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb', line 15

def call
  return [] if inventory_items.empty?

  keys = pool_keys
  counts = SpreeCmCommissioner.inventory_redis_pool.with { |redis| redis.mget(*keys.flatten) }
  counts_by_pool = counts.each_slice(inventory_items.length).to_a

  inventory_items.map.with_index { |item, i| build_cached_item(item, i, keys, counts_by_pool) }
end