class Canari::DomainCache

Store and retrieve domains from Memcached

Public Class Methods

cache() click to toggle source
# File lib/canari/domain_cache.rb, line 37
def cache
  return @cache if @cache

  mconfig = Canari.config[:memcached]
  @cache = Dalli::Client.new("#{mconfig[:host]}:#{mconfig[:port]}",
                             namespace: mconfig[:namespace])
end
fetch(domains) click to toggle source
# File lib/canari/domain_cache.rb, line 20
def fetch(domains)
  keys = variants(domains)
  cache.get_multi(keys).keys
end
preload(domains_file) click to toggle source
# File lib/canari/domain_cache.rb, line 7
def preload(domains_file)
  cache.flush
  count = 0
  File.readlines(File.expand_path(domains_file)).each do |line|
    line = line.strip
    next if line.start_with?('#')

    cache.set(line, 1)
    count += 1
  end
  Canari.logger.info "Preloaded #{count} domains"
end
variants(domains) click to toggle source
# File lib/canari/domain_cache.rb, line 25
def variants(domains)
  domains.map do |domain|
    domain = domain.split('.').reject { |part| part == '*' }.join('.')
    [domain].tap do |values|
      until domain.empty?
        domain = domain.split('.').drop(1).join('.')
        values << domain unless domain.empty?
      end
    end
  end.flatten.uniq
end