Sha256: 86cb4776eb0bfb54baeb8f52725fcd2e482c184a51a3594bc7699578deafd1dd

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Canari
  # Store and retrieve domains from Memcached
  class DomainCache
    class << self
      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

      def fetch(domains)
        keys = variants(domains)
        cache.get_multi(keys).keys
      end

      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

      def cache
        return @cache if @cache

        mconfig = Canari.config[:memcached]
        @cache = Dalli::Client.new("#{mconfig[:host]}:#{mconfig[:port]}",
                                   namespace: mconfig[:namespace])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
canari-0.2.0 lib/canari/domain_cache.rb
canari-0.1.0 lib/canari/domain_cache.rb