Class: EmailDomainChecker::Cache::RedisAdapter

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

Overview

Redis cache adapter Requires the ‘redis’ gem to be available

Instance Method Summary collapse

Methods inherited from BaseAdapter

#with

Constructor Details

#initialize(redis_client = nil) ⇒ RedisAdapter

Returns a new instance of RedisAdapter.



10
11
12
# File 'lib/email_domain_checker/cache/redis_adapter.rb', line 10

def initialize(redis_client = nil)
  @redis = redis_client || create_default_redis_client
end

Instance Method Details

#clearObject



43
44
45
46
47
# File 'lib/email_domain_checker/cache/redis_adapter.rb', line 43

def clear
  @redis.flushdb
rescue Redis::BaseError
  # Silently fail if Redis is unavailable
end

#delete(key) ⇒ Object



37
38
39
40
41
# File 'lib/email_domain_checker/cache/redis_adapter.rb', line 37

def delete(key)
  @redis.del(key)
rescue Redis::BaseError
  # Silently fail if Redis is unavailable
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/email_domain_checker/cache/redis_adapter.rb', line 49

def exists?(key)
  @redis.exists?(key)
rescue Redis::BaseError
  false
end

#get(key) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/email_domain_checker/cache/redis_adapter.rb', line 14

def get(key)
  value = @redis.get(key)
  return nil unless value

  # Parse JSON value
  JSON.parse(value)
rescue JSON::ParserError, Redis::BaseError
  nil
end

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/email_domain_checker/cache/redis_adapter.rb', line 24

def set(key, value, ttl: nil)
  json_value = JSON.generate(value)
  if ttl
    @redis.setex(key, ttl, json_value)
  else
    @redis.set(key, json_value)
  end
  value
rescue Redis::BaseError
  # Silently fail if Redis is unavailable
  value
end