Skip to content

Commit

Permalink
perf: add cache on display info
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Sep 10, 2019
1 parent c30fa1f commit a39f282
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/models/cell_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def node_output

def flush_cache
Rails.cache.delete("previous_cell_output/#{tx_hash}/#{cell_index}")
Rails.cache.delete_matched("normal_tx_display_inputs_previews_*_#{ckb_transaction_id}")
Rails.cache.delete_matched("normal_tx_display_outputs_previews_*_#{ckb_transaction_id}")
end
end

Expand Down
18 changes: 11 additions & 7 deletions app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ def income(address)
private

def normal_tx_display_outputs(previews)
cell_outputs_for_display = previews ? outputs.limit(10) : outputs
cell_outputs_for_display.order(:id).map do |output|
consumed_tx_hash = output.live? ? nil : output.consumed_by.tx_hash
{ id: output.id, capacity: output.capacity, address_hash: output.address_hash, status: output.status, consumed_tx_hash: consumed_tx_hash}
Rails.cache.fetch("normal_tx_display_outputs_previews_#{previews}_#{id}", race_condition_ttl: 3.seconds) do
cell_outputs_for_display = previews ? outputs.limit(10) : outputs
cell_outputs_for_display.order(:id).map do |output|
consumed_tx_hash = output.live? ? nil : output.consumed_by.tx_hash
{ id: output.id, capacity: output.capacity, address_hash: output.address_hash, status: output.status, consumed_tx_hash: consumed_tx_hash }
end
end
end

Expand All @@ -69,9 +71,11 @@ def cellbase_display_outputs
end

def normal_tx_display_inputs(previews)
cell_inputs_for_display = previews ? inputs.limit(10) : inputs
cell_inputs_for_display.order(:id).map do |input|
{ id: input.id, from_cellbase: false, capacity: input.capacity, address_hash: input.address_hash, generated_tx_hash: tx_hash }
Rails.cache.fetch("normal_tx_display_inputs_previews_#{previews}_#{id}", race_condition_ttl: 3.seconds) do
cell_inputs_for_display = previews ? inputs.limit(10) : inputs
cell_inputs_for_display.order(:id).map do |input|
{ id: input.id, from_cellbase: false, capacity: input.capacity, address_hash: input.address_hash, generated_tx_hash: tx_hash }
end
end
end

Expand Down

0 comments on commit a39f282

Please sign in to comment.