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

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

Overview

Manages all cache operations with a clean, unified interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CacheManager.

Parameters:

  • config (defaults to: nil)

    cache configuration (see CacheConfiguration.from_config)

  • client (Lutaml::Hal::Client, nil) (defaults to: nil)

    used to canonicalize relative URLs so that a resource fetched by endpoint path and the same resource realized from an absolute link href share a cache entry.



29
30
31
32
33
34
35
36
37
38
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 29

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.



23
24
25
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 23

def cache_store
  @cache_store
end

#configurationObject (readonly)

Returns the value of attribute configuration.



23
24
25
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 23

def configuration
  @configuration
end

Instance Method Details

#available?Boolean

Check if cache is available and configured

Returns:

  • (Boolean)


129
130
131
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 129

def available?
  !cache_store.nil?
end

#clearObject

Clear all cache entries



97
98
99
100
101
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 97

def clear
  return unless cache_store

  cache_store.clear
end

#conditional_request_headers(url) ⇒ Object

Make a conditional request using cached metadata



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

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

  entry.conditional_headers
end

#get(url) ⇒ Object

Get a cache entry by URL



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 41

def get(url)
  return nil unless cache_store

  key = cache_key(url)

  if http_aware_cache?
    get_from_http_cache(url, key)
  else
    get_from_basic_cache(key)
  end
end

#http_aware_cache?Boolean

Check if using HTTP-aware cache

Returns:

  • (Boolean)


134
135
136
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 134

def http_aware_cache?
  configuration.http_aware? && cache_store.respond_to?(:fetch)
end

#infoObject

Get cache information



117
118
119
120
121
122
123
124
125
126
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 117

def info
  return nil unless cache_store

  {
    adapter_type: cache_store.class.name,
    configuration: configuration,
    current_size: cache_store.respond_to?(:size) ? cache_store.size : 'unknown',
    stats: stats
  }
end

#invalidate(url) ⇒ Object

Remove a specific cache entry



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

def invalidate(url)
  return unless cache_store

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

#refresh_entry(url, response) ⇒ Object

Update cache entry after a 304 Not Modified response



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

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

  entry.(response)
  set_refreshed_entry(url, entry)
end

#set(url, response, hal_resource) ⇒ Object

Store a cache entry



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 54

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)

  if http_aware_cache?
    set_in_http_cache(key, entry, response)
  else
    set_in_basic_cache(key, entry)
  end

  entry
end

#statsObject

Get cache statistics



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lutaml/hal/cache/cache_manager.rb', line 104

def stats
  return {} unless cache_store

  if cache_store.respond_to?(:cache_info)
    cache_store.cache_info
  elsif cache_store.respond_to?(:stats)
    cache_store.stats
  else
    {}
  end
end