Class: WdsImageCache
- Inherits:
-
Object
- Object
- WdsImageCache
- Defined in:
- app/services/wds_image_cache.rb
Instance Attribute Summary collapse
-
#cache_duration ⇒ Object
Returns the value of attribute cache_duration.
-
#wds_server ⇒ Object
Returns the value of attribute wds_server.
Instance Method Summary collapse
- #cache(key, &block) ⇒ Object
- #cache_scope ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(wds_server, cache_duration: 180.minutes) ⇒ WdsImageCache
constructor
A new instance of WdsImageCache.
- #read(key) ⇒ Object
- #refresh ⇒ Object
- #write(key, value) ⇒ Object
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_duration ⇒ Object
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_server ⇒ Object
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_scope ⇒ Object
41 42 43 |
# File 'app/services/wds_image_cache.rb', line 41 def cache_scope Rails.cache.fetch(cache_scope_key, ) { 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, ) end |
#refresh ⇒ Object
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, ) end |