Class: Allgood::CacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/allgood/cache_store.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCacheStore

Returns a new instance of CacheStore.



9
10
11
# File 'lib/allgood/cache_store.rb', line 9

def initialize
  @memory_store = {}
end

Class Method Details

.instanceObject



5
6
7
# File 'lib/allgood/cache_store.rb', line 5

def self.instance
  @instance ||= new
end

Instance Method Details

#cleanup_old_keysObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/allgood/cache_store.rb', line 30

def cleanup_old_keys
  return unless rails_cache_available?

  keys_pattern = "allgood:*"
  if Rails.cache.respond_to?(:delete_matched)
    Rails.cache.delete_matched("#{keys_pattern}:*:#{(Time.current - 2.days).strftime('%Y-%m-%d')}*")
  end
rescue StandardError => e
  Rails.logger.warn "Allgood: Failed to cleanup old cache keys: #{e.message}"
end

#read(key) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/allgood/cache_store.rb', line 13

def read(key)
  if rails_cache_available?
    Rails.cache.read(key)
  else
    @memory_store[key]
  end
end

#write(key, value) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/allgood/cache_store.rb', line 21

def write(key, value)
  if rails_cache_available?
    expiry = key.include?('day') ? 1.day : 1.hour
    Rails.cache.write(key, value, expires_in: expiry)
  else
    @memory_store[key] = value
  end
end