Class: Lutaml::Hal::Cache::CacheManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/hal/cache/cache_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, client: nil) ⇒ CacheManager

Returns a new instance of CacheManager.



11
12
13
14
15
16
17
18
19
20
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 11

def initialize(config = nil, client: nil)
  @client = client
  @configuration = CacheConfiguration.from_config(config)
  begin
    @configuration.validate!
  rescue ArgumentError => e
    raise ArgumentError, "Invalid cache configuration: #{e.message}"
  end
  @cache_store = create_cache_store
end

Instance Attribute Details

#cache_storeObject (readonly)

Returns the value of attribute cache_store.



9
10
11
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 9

def cache_store
  @cache_store
end

#configurationObject (readonly)

Returns the value of attribute configuration.



9
10
11
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 9

def configuration
  @configuration
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 89

def available?
  !cache_store.nil?
end

#clearObject



66
67
68
69
70
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 66

def clear
  return unless cache_store

  cache_store.clear
end

#conditional_request_headers(url) ⇒ Object



43
44
45
46
47
48
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 43

def conditional_request_headers(url)
  entry = get(url)
  return {} unless entry&.revalidatable?

  entry.conditional_headers
end

#get(url) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 22

def get(url)
  return nil unless cache_store

  key = cache_key(url)
  raw = cache_store.get(key)
  return nil unless raw

  deserialize_entry(raw)
end

#infoObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 78

def info
  return nil unless cache_store

  {
    adapter_type: cache_store.class.name,
    configuration: configuration,
    current_size: cache_store.size,
    stats: stats
  }
end

#invalidate(url) ⇒ Object



59
60
61
62
63
64
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 59

def invalidate(url)
  return unless cache_store

  key = cache_key(url)
  cache_store.delete(key)
end

#refresh_entry(url, response) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 50

def refresh_entry(url, response)
  entry = get(url)
  return unless entry

  entry.(response)
  key = cache_key(url)
  cache_store.set(key, entry.to_storage_h)
end

#set(url, response, hal_resource) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 32

def set(url, response, hal_resource)
  return unless cache_store

  entry = CacheEntry.create(url, response, hal_resource)
  return unless entry.cacheable?

  key = cache_key(url)
  cache_store.set(key, entry.to_storage_h)
  entry
end

#statsObject



72
73
74
75
76
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 72

def stats
  return {} unless cache_store

  cache_store.cache_info
end