Class: Lutaml::Hal::Cache::SimpleCacheStore

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

Overview

Simple in-memory cache store for testing and fallback scenarios

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 100) ⇒ SimpleCacheStore

Returns a new instance of SimpleCacheStore.



10
11
12
13
14
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 10

def initialize(max_size = 100)
  @max_size = max_size
  @cache = {}
  @access_order = []
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



8
9
10
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 8

def max_size
  @max_size
end

Instance Method Details

#cache_infoObject



62
63
64
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 62

def cache_info
  stats
end

#clearObject



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

def clear
  @cache.clear
  @access_order.clear
end

#delete(key) ⇒ Object



40
41
42
43
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 40

def delete(key)
  @access_order.delete(key)
  @cache.delete(key)
end

#get(key) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 16

def get(key)
  return nil unless @cache.key?(key)

  # Update access order for LRU
  @access_order.delete(key)
  @access_order.push(key)

  @cache[key]
end

#set(key, value) ⇒ Object



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

def set(key, value)
  # Remove existing entry if present
  if @cache.key?(key)
    @access_order.delete(key)
  elsif @cache.size >= @max_size
    # Evict least recently used item
    lru_key = @access_order.shift
    @cache.delete(lru_key)
  end

  @cache[key] = value
  @access_order.push(key)
end

#sizeObject



50
51
52
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 50

def size
  @cache.size
end

#statsObject



54
55
56
57
58
59
60
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 54

def stats
  {
    size: @cache.size,
    max_size: @max_size,
    keys: @cache.keys
  }
end