Use new DTIRequests.load_many
helper in Item.preload_nc_trade_values
This commit is contained in:
parent
1d4771ecc5
commit
2be21a9238
1 changed files with 2 additions and 16 deletions
|
@ -1,6 +1,3 @@
|
||||||
require "async"
|
|
||||||
require "async/barrier"
|
|
||||||
|
|
||||||
class Item < ApplicationRecord
|
class Item < ApplicationRecord
|
||||||
include PrettyParam
|
include PrettyParam
|
||||||
include Item::Dyeworks
|
include Item::Dyeworks
|
||||||
|
@ -671,21 +668,10 @@ class Item < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.preload_nc_trade_values(items)
|
def self.preload_nc_trade_values(items)
|
||||||
# Only allow 10 trade values to be loaded at a time.
|
DTIRequests.load_many(max_at_once: 10) do |task|
|
||||||
barrier = Async::Barrier.new
|
|
||||||
semaphore = Async::Semaphore.new(10, parent: barrier)
|
|
||||||
|
|
||||||
Sync do
|
|
||||||
# Load all the trade values in concurrent async tasks. (The
|
# Load all the trade values in concurrent async tasks. (The
|
||||||
# `nc_trade_value` caches the value in the Item object.)
|
# `nc_trade_value` caches the value in the Item object.)
|
||||||
items.each do |item|
|
items.each { |item| task.async { item.nc_trade_value } }
|
||||||
semaphore.async { item.nc_trade_value }
|
|
||||||
end
|
|
||||||
|
|
||||||
# Wait until all tasks are done.
|
|
||||||
barrier.wait
|
|
||||||
ensure
|
|
||||||
barrier.stop # If something goes wrong, clean up all tasks.
|
|
||||||
end
|
end
|
||||||
|
|
||||||
items
|
items
|
||||||
|
|
Loading…
Reference in a new issue