about summary refs log tree commit diff
path: root/app/models/concerns/domain_materializable.rb
blob: 0eac6878ed4cec28ab09713573ca73164c92f9e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# frozen_string_literal: true

module DomainMaterializable
  extend ActiveSupport::Concern

  include Redisable

  included do
    after_create_commit :refresh_instances_view
  end

  def refresh_instances_view
    return if domain.nil? || Instance.exists?(domain: domain)

    Instance.refresh
    count_unique_subdomains!
  end

  def count_unique_subdomains!
    second_and_top_level_domain = PublicSuffix.domain(domain, ignore_private: true)
    with_redis do |redis|
      redis.pfadd("unique_subdomains_for:#{second_and_top_level_domain}", domain)
      redis.expire("unique_subdomains_for:#{second_and_top_level_domain}", 1.minute.seconds)
    end
  end
end