Skip to content

Commit 1f1d5c5

Browse files
authored
[ᚬmaster] chore: fill epoch number to block statistic and perf f… (#548)
[ᚬmaster] chore: fill epoch number to block statistic and perf flush cache
2 parents 1e4d811 + 3dd809e commit 1f1d5c5

File tree

10 files changed

+49
-26
lines changed

10 files changed

+49
-26
lines changed

app/models/address.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ def cached_lock_script
4646
end
4747

4848
def flush_cache
49-
Rails.cache.delete([self.class.name, address_hash])
50-
Rails.cache.delete([self.class.name, lock_hash])
49+
$redis.pipelined do
50+
$redis.del(*cache_keys)
51+
end
52+
end
53+
54+
def cache_keys
55+
%W(#{self.class.name}/#{address_hash} #{self.class.name}/#{lock_hash})
5156
end
5257

5358
def special?

app/models/block.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ def miner_address
8484
end
8585

8686
def flush_cache
87-
Rails.cache.delete([self.class.name, block_hash])
88-
Rails.cache.delete([self.class.name, number])
87+
$redis.pipelined do
88+
$redis.del(*cache_keys)
89+
end
90+
end
91+
92+
def cache_keys
93+
%W(#{self.class.name}/#{block_hash} #{self.class.name}/#{number})
8994
end
9095

9196
def invalid!

app/models/cell_input.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ def self.cached_find(id)
3333
Rails.cache.realize([name, id], race_condition_ttl: 3.seconds) { find(id) }
3434
end
3535

36+
def cache_keys
37+
%W(CellInput/#{id}/lock_script CellInput/#{id}/type_script)
38+
end
39+
3640
def flush_cache
37-
Rails.cache.delete_matched("CellInput/#{id}*")
38-
Rails.cache.delete_matched("previous_cell_output*")
41+
$redis.pipelined do
42+
$redis.del(*cache_keys)
43+
end
3944
end
4045

4146
private
@@ -46,7 +51,7 @@ def previous_cell_output!
4651
tx_hash = previous_output["tx_hash"]
4752
cell_index = previous_output["index"].to_i
4853

49-
Rails.cache.realize("previous_cell_output/#{tx_hash}/#{cell_index}", race_condition_ttl: 3.seconds) do
54+
Rails.cache.realize("cell_input_#{id}/previous_cell_output/#{tx_hash}/#{cell_index}", race_condition_ttl: 3.seconds) do
5055
CellOutput.find_by!(tx_hash: tx_hash, cell_index: cell_index)
5156
end
5257
end

app/models/cell_output.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ def node_output
2828
CKB::Types::Output.new(capacity: capacity.to_i, lock: lock, type: type)
2929
end
3030

31+
def cache_keys
32+
%W(previous_cell_output/#{tx_hash}/#{cell_index} normal_tx_display_inputs_previews_true_#{ckb_transaction_id}
33+
normal_tx_display_inputs_previews_false_#{ckb_transaction_id} normal_tx_display_inputs_previews_true_#{consumed_by_id}
34+
normal_tx_display_inputs_previews_false_#{consumed_by_id} normal_tx_display_outputs_previews_true_#{ckb_transaction_id}
35+
normal_tx_display_outputs_previews_false_#{ckb_transaction_id} normal_tx_display_outputs_previews_true_#{consumed_by_id}
36+
normal_tx_display_outputs_previews_false_#{consumed_by_id}
37+
)
38+
end
39+
3140
def flush_cache
32-
Rails.cache.delete("previous_cell_output/#{tx_hash}/#{cell_index}")
33-
Rails.cache.delete_matched("normal_tx_display_inputs_previews_*_#{ckb_transaction_id}")
34-
Rails.cache.delete_matched("normal_tx_display_inputs_previews_*_#{consumed_by_id}")
35-
Rails.cache.delete_matched("normal_tx_display_outputs_previews_*_#{ckb_transaction_id}")
36-
Rails.cache.delete_matched("normal_tx_display_outputs_previews_*_#{consumed_by_id}")
41+
$redis.pipelined do
42+
$redis.del(*cache_keys)
43+
end
3744
end
3845
end
3946

app/models/ckb_sync/node_data_processor.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,10 @@ def build_type_script(cell_output, type_script)
454454
end
455455

456456
def update_tx_fee_related_data(local_block, input_capacities)
457-
updated_inputs = []
458-
updated_outputs = []
459-
account_books = []
460457
local_block.cell_inputs.where(from_cell_base: false, previous_cell_output_id: nil).find_in_batches(batch_size: 3500) do |cell_inputs|
458+
updated_inputs = []
459+
updated_outputs = []
460+
account_books = []
461461
ApplicationRecord.transaction do
462462
cell_inputs.each do |cell_input|
463463
consumed_tx = cell_input.ckb_transaction
@@ -479,8 +479,17 @@ def update_tx_fee_related_data(local_block, input_capacities)
479479
CellInput.import!(updated_inputs, validate: false, on_duplicate_key_update: [:previous_cell_output_id])
480480
CellOutput.import!(updated_outputs, validate: false, on_duplicate_key_update: [:consumed_by_id, :status, :consumed_block_timestamp])
481481
AccountBook.import!(account_books, validate: false)
482-
updated_inputs.map(&:flush_cache)
483-
updated_outputs.map(&:flush_cache)
482+
end
483+
input_cache_keys = updated_inputs.map(&:cache_keys)
484+
output_cache_keys = updated_outputs.map(&:cache_keys)
485+
flush_caches(input_cache_keys + output_cache_keys)
486+
end
487+
end
488+
489+
def flush_caches(cache_keys)
490+
cache_keys.each_slice(400) do |keys|
491+
$redis.pipelined do
492+
$redis.del(*keys)
484493
end
485494
end
486495
end

app/services/charts/block_statistic_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def call
1313
live_cells_count = CellOutput.live.count
1414
dead_cells_count = CellOutput.dead.count
1515
block_statistic = ::BlockStatistic.find_or_create_by(block_number: block_number)
16-
block_statistic.update(difficulty: target_block.difficulty, hash_rate: hash_rate, live_cells_count: live_cells_count, dead_cells_count: dead_cells_count)
16+
block_statistic.update(epoch_number: target_block.epoch, difficulty: target_block.difficulty, hash_rate: hash_rate, live_cells_count: live_cells_count, dead_cells_count: dead_cells_count)
1717
end
1818

1919
private

app/workers/charts/block_statistic.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module Charts
22
class BlockStatistic
33
include Sidekiq::Worker
4-
sidekiq_options unique: :until_executed
5-
sidekiq_options retry: false
64
sidekiq_options queue: "critical"
75

86
def perform

app/workers/charts/daily_statistic.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module Charts
22
class DailyStatistic
33
include Sidekiq::Worker
4-
sidekiq_options unique: :until_executed
5-
sidekiq_options retry: false
64
sidekiq_options queue: "critical"
75

86
def perform(datetime = nil)

app/workers/charts/epoch_statistic.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module Charts
22
class EpochStatistic
33
include Sidekiq::Worker
4-
sidekiq_options unique: :until_executed
5-
sidekiq_options retry: false
64
sidekiq_options queue: "critical"
75

86
def perform

app/workers/charts/forked_event_processor.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module Charts
22
class ForkedEventProcessor
33
include Sidekiq::Worker
4-
sidekiq_options unique: :until_executed
5-
sidekiq_options retry: false
64
sidekiq_options queue: "critical"
75

86
def perform

0 commit comments

Comments
 (0)