Class: WdsImageCache

Inherits:
Object
  • Object
show all
Defined in:
app/services/wds_image_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wds_server, cache_duration: 180.minutes) ⇒ WdsImageCache

Returns a new instance of WdsImageCache.



6
7
8
9
# File 'app/services/wds_image_cache.rb', line 6

def initialize(wds_server, cache_duration: 180.minutes)
  self.wds_server = wds_server
  self.cache_duration = cache_duration
end

Instance Attribute Details

#cache_durationObject

Returns the value of attribute cache_duration.



2
3
4
# File 'app/services/wds_image_cache.rb', line 2

def cache_duration
  @cache_duration
end

#wds_serverObject

Returns the value of attribute wds_server.



2
3
4
# File 'app/services/wds_image_cache.rb', line 2

def wds_server
  @wds_server
end

Instance Method Details

#cache(key, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'app/services/wds_image_cache.rb', line 11

def cache(key, &block)
  cached_value = read(key)
  return cached_value if cached_value
  return unless block_given?

  uncached_value = get_uncached_value(&block)
  write(key, uncached_value)
  uncached_value
end

#cache_scopeObject



41
42
43
# File 'app/services/wds_image_cache.rb', line 41

def cache_scope
  Rails.cache.fetch(cache_scope_key, cache_options) { Foreman.uuid }
end

#delete(key) ⇒ Object



21
22
23
# File 'app/services/wds_image_cache.rb', line 21

def delete(key)
  Rails.cache.delete(cache_key + key.to_s)
end

#read(key) ⇒ Object



25
26
27
# File 'app/services/wds_image_cache.rb', line 25

def read(key)
  Rails.cache.read(cache_key + key.to_s, cache_options)
end

#refreshObject



33
34
35
36
37
38
39
# File 'app/services/wds_image_cache.rb', line 33

def refresh
  Rails.cache.delete(cache_scope_key)
  true
rescue StandardError => e
  logger.exception('Failed to refresh the WDS image cache', e)
  false
end

#write(key, value) ⇒ Object



29
30
31
# File 'app/services/wds_image_cache.rb', line 29

def write(key, value)
  Rails.cache.write(cache_key + key.to_s, value, cache_options)
end