Skip to content

Commit

Permalink
perf: use cached_find on suggest query
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Jul 18, 2019
1 parent 76949ee commit d6183fb
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions app/models/suggest_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,43 @@ def find!
def find_record_by_query_key!
result =
if QueryKeyUtils.integer_string?(query_key)
find_block_by_number
find_cached_block
elsif QueryKeyUtils.valid_hex?(query_key)
find_by_hex
elsif QueryKeyUtils.valid_address?(query_key)
find_address_by_hash
find_cached_address
end

raise ActiveRecord::RecordNotFound if result.blank?

result
end

def find_block_by_number
block = Block.where(number: query_key).available.first
def find_cached_block
block = Block.cached_find(query_key)
raise Api::V1::Exceptions::BlockNotFoundError if block.blank?

BlockSerializer.new(block)
end

def find_block_by_hash
block = Block.where(block_hash: query_key).available.first
BlockSerializer.new(block) if block.present?
block
end

def find_ckb_transaction_by_hash
ckb_transaction = CkbTransaction.cached_find(query_key)
CkbTransactionSerializer.new(ckb_transaction) if ckb_transaction.present?
end

def find_address_by_hash
address = Address.find_by(address_hash: query_key)
def find_address_by_lock_hash
address = Address.cached_find(query_key)
LockHashSerializer.new(address) if address.present?
end

def find_cached_address
address = Address.cached_find(query_key)
raise Api::V1::Exceptions::AddressNotFoundError if address.blank?

AddressSerializer.new(address)
end

def find_address_by_lock_hash
address = Address.find_by(lock_hash: query_key)
LockHashSerializer.new(address) if address.present?
end

def find_by_hex
find_block_by_hash || find_ckb_transaction_by_hash || find_address_by_lock_hash
Block.cached_find(query_key) || find_ckb_transaction_by_hash || find_address_by_lock_hash
end
end

0 comments on commit d6183fb

Please sign in to comment.