Class: EmailDomainChecker::Cache::BaseAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/email_domain_checker/cache/base_adapter.rb

Overview

Base class for cache adapters All cache adapters must implement these methods

Direct Known Subclasses

MemoryAdapter, RedisAdapter

Instance Method Summary collapse

Instance Method Details

#clearvoid

This method returns an undefined value.

Clear all cache entries

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 33

def clear
  raise NotImplementedError, "Subclasses must implement #clear"
end

#delete(key) ⇒ void

This method returns an undefined value.

Delete a key from cache

Parameters:

  • key (String)

    cache key

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 27

def delete(key)
  raise NotImplementedError, "Subclasses must implement #delete"
end

#exists?(key) ⇒ Boolean

Check if a key exists in cache

Parameters:

  • key (String)

    cache key

Returns:

  • (Boolean)

    true if key exists



40
41
42
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 40

def exists?(key)
  get(key) != nil
end

#get(key) ⇒ Object?

Get cached value for a key

Parameters:

  • key (String)

    cache key

Returns:

  • (Object, nil)

    cached value or nil if not found

Raises:

  • (NotImplementedError)


11
12
13
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 11

def get(key)
  raise NotImplementedError, "Subclasses must implement #get"
end

#set(key, value, ttl: nil) ⇒ void

This method returns an undefined value.

Set a value in cache

Parameters:

  • key (String)

    cache key

  • value (Object)

    value to cache

  • ttl (Integer, nil) (defaults to: nil)

    time to live in seconds (nil for no expiration)

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 20

def set(key, value, ttl: nil)
  raise NotImplementedError, "Subclasses must implement #set"
end

#with(key, ttl: nil, force: false) { ... } ⇒ Object

Fetch value from cache or execute block and cache the result Similar to Rails.cache.fetch

Parameters:

  • key (String)

    cache key

  • ttl (Integer, nil) (defaults to: nil)

    time to live in seconds (nil for no expiration)

  • force (Boolean) (defaults to: false)

    if true, always execute block and update cache

Yields:

  • Block to execute when cache miss

Returns:

  • (Object)

    cached value or block result

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 51

def with(key, ttl: nil, force: false, &block)
  raise ArgumentError, "Block is required" unless block_given?

  # Return cached value if not forcing and cache exists
  unless force
    return get(key) if exists?(key)
  end

  # Execute block and cache the result
  # Rails.cache.fetch also caches nil values, so we do the same
  value = yield
  set(key, value, ttl: ttl)
  value
end