Skip to content

Commit

Permalink
Improve performance of list of blocked domains by caching counts (mas…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored Mar 25, 2019
1 parent 7673c25 commit 98d395e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/models/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ class Instance

def initialize(resource)
@domain = resource.domain
@accounts_count = resource.accounts_count
@accounts_count = resource.is_a?(DomainBlock) ? nil : resource.accounts_count
@domain_block = resource.is_a?(DomainBlock) ? resource : DomainBlock.find_by(domain: domain)
end

def cached_sample_accounts
Rails.cache.fetch("#{cache_key}/sample_accounts", expires_in: 12.hours) { Account.where(domain: domain).searchable.joins(:account_stat).popular.limit(3) }
end

def cached_accounts_count
@accounts_count || Rails.cache.fetch("#{cache_key}/count", expires_in: 12.hours) { Account.where(domain: domain).count }
end

def to_param
domain
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/instances/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
%h4
= instance.domain
%small
= t('admin.instances.known_accounts', count: instance.accounts_count)
= t('admin.instances.known_accounts', count: instance.cached_accounts_count)

- if instance.domain_block
- if !instance.domain_block.noop?
Expand Down

0 comments on commit 98d395e

Please sign in to comment.