Class: Eld::Cache::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/eld/cache/redis.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:, cache_key: "eld/certificates") ⇒ Redis

Returns a new instance of Redis.



6
7
8
9
10
11
12
# File 'lib/eld/cache/redis.rb', line 6

def initialize(
  client:,
  cache_key: "eld/certificates"
)
  @client = client
  @cache_key = cache_key
end

Instance Method Details

#clearObject



39
40
41
42
43
# File 'lib/eld/cache/redis.rb', line 39

def clear
  @client.del(@cache_key)

  nil
end

#fetch(&block) ⇒ Object



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

def fetch(&block)
  data = @client.get(@cache_key)

  if data
    JSON.parse(data)
  else
    set(&block)
  end
end

#set(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eld/cache/redis.rb', line 24

def set(&block)
  response = block.call

  # We calculate ex in seconds instead of using exat for
  # specs. Not ideal but good enough.
  # Probably should mock redis calls in tests.
  @client.set(
    @cache_key,
    response[:data].to_json,
    ex: response[:expires_at] - Time.now.utc.to_i
  )

  response[:data]
end