Module: EmailDomainChecker::Cache

Defined in:
lib/email_domain_checker/cache.rb,
lib/email_domain_checker/cache/base_adapter.rb,
lib/email_domain_checker/cache/redis_adapter.rb,
lib/email_domain_checker/cache/memory_adapter.rb

Defined Under Namespace

Classes: BaseAdapter, MemoryAdapter, RedisAdapter

Class Method Summary collapse

Class Method Details

.create_adapter(type: :memory, redis_client: nil) ⇒ BaseAdapter

Factory method to create cache adapter based on configuration

Parameters:

  • type (Symbol, Class, Object) (defaults to: :memory)

    Cache adapter type, class, or instance

    • Symbol: :memory or :redis

    • Class: A custom adapter class (must inherit from BaseAdapter)

    • Object: A custom adapter instance (must be an instance of BaseAdapter)

  • redis_client (Redis, nil) (defaults to: nil)

    Redis client instance (only used for :redis type)

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/email_domain_checker/cache.rb', line 25

def self.create_adapter(type: :memory, redis_client: nil)
  # If type is already an adapter instance, return it
  return type if type.is_a?(BaseAdapter)

  # If type is a Class, instantiate it
  if type.is_a?(Class)
    unless type < BaseAdapter
      raise ArgumentError, "Custom cache adapter class must inherit from EmailDomainChecker::Cache::BaseAdapter"
    end
    return type.new
  end

  # Handle symbol types
  case type.to_sym
  when :memory
    MemoryAdapter.new
  when :redis
    if defined?(RedisAdapter)
      RedisAdapter.new(redis_client)
    else
      # Fallback to memory if Redis is not available
      warn "Redis adapter requested but 'redis' gem is not available. Falling back to memory cache."
      MemoryAdapter.new
    end
  else
    raise ArgumentError, "Unknown cache adapter type: #{type}. Available: :memory, :redis, or a custom BaseAdapter class/instance"
  end
end