Class: Takagi::EventBus::LRUCache

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/event_bus/lru_cache.rb

Overview

Thread-safe LRU cache with TTL (pure Ruby) Zero runtime dependencies

Features:

  • Least Recently Used eviction when at capacity

  • Time-To-Live (TTL) based expiration

  • Thread-safe with Mutex

Examples:

cache = LRUCache.new(max_size: 1000, ttl: 3600)
cache.set('key', 'value')
cache.get('key') # => 'value'
cache.size # => 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 1000, ttl = 3600) ⇒ LRUCache

Initialize LRU cache

Parameters:

  • max_size (Integer) (defaults to: 1000)

    Maximum number of entries (default: 1000)

  • ttl (Integer) (defaults to: 3600)

    Time-to-live in seconds (default: 3600)



24
25
26
27
28
29
30
31
# File 'lib/takagi/event_bus/lru_cache.rb', line 24

def initialize(max_size = 1000, ttl = 3600)
  @max_size = max_size
  @ttl = ttl
  @cache = {}
  @access_order = []
  @timestamps = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



19
20
21
# File 'lib/takagi/event_bus/lru_cache.rb', line 19

def max_size
  @max_size
end

#ttlObject (readonly)

Returns the value of attribute ttl.



19
20
21
# File 'lib/takagi/event_bus/lru_cache.rb', line 19

def ttl
  @ttl
end

Instance Method Details

#clearObject

Clear all entries



80
81
82
83
84
85
86
# File 'lib/takagi/event_bus/lru_cache.rb', line 80

def clear
  @mutex.synchronize do
    @cache.clear
    @access_order.clear
    @timestamps.clear
  end
end

#delete(key) ⇒ Object?

Delete entry from cache

Parameters:

  • key (Object)

    Cache key

Returns:

  • (Object, nil)

    Deleted value or nil



71
72
73
74
75
76
77
# File 'lib/takagi/event_bus/lru_cache.rb', line 71

def delete(key)
  @mutex.synchronize do
    @access_order.delete(key)
    @timestamps.delete(key)
    @cache.delete(key)
  end
end

#empty?Boolean

Check if cache is empty

Returns:

  • (Boolean)


96
97
98
# File 'lib/takagi/event_bus/lru_cache.rb', line 96

def empty?
  @mutex.synchronize { @cache.empty? }
end

#get(key) ⇒ Object?

Get value from cache

Parameters:

  • key (Object)

    Cache key

Returns:

  • (Object, nil)

    Cached value or nil if not found/expired



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/takagi/event_bus/lru_cache.rb', line 36

def get(key)
  @mutex.synchronize do
    cleanup_expired
    return nil unless @cache.key?(key)

    # Update access order (move to end = most recently used)
    @access_order.delete(key)
    @access_order << key
    @timestamps[key] = Time.now

    @cache[key]
  end
end

#key?(key) ⇒ Boolean

Check if key exists in cache

Parameters:

  • key (Object)

    Cache key

Returns:

  • (Boolean)


103
104
105
106
107
108
# File 'lib/takagi/event_bus/lru_cache.rb', line 103

def key?(key)
  @mutex.synchronize do
    cleanup_expired
    @cache.key?(key)
  end
end

#keysArray

Get all keys in cache

Returns:

  • (Array)

    Cache keys



112
113
114
115
116
117
# File 'lib/takagi/event_bus/lru_cache.rb', line 112

def keys
  @mutex.synchronize do
    cleanup_expired
    @cache.keys
  end
end

#set(key, value) ⇒ Object

Set value in cache

Parameters:

  • key (Object)

    Cache key

  • value (Object)

    Value to cache



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/takagi/event_bus/lru_cache.rb', line 53

def set(key, value)
  @mutex.synchronize do
    cleanup_expired

    # Remove oldest entry if at capacity and key is new
    evict_oldest if @cache.size >= @max_size && !@cache.key?(key)

    # Add/update entry
    @access_order.delete(key) # Remove if exists
    @access_order << key      # Add to end (most recently used)
    @cache[key] = value
    @timestamps[key] = Time.now
  end
end

#sizeInteger

Get number of entries in cache

Returns:

  • (Integer)

    Cache size



90
91
92
# File 'lib/takagi/event_bus/lru_cache.rb', line 90

def size
  @mutex.synchronize { @cache.size }
end

#statsHash

Get cache statistics

Returns:

  • (Hash)

    Statistics



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/takagi/event_bus/lru_cache.rb', line 121

def stats
  @mutex.synchronize do
    cleanup_expired
    {
      size: @cache.size,
      max_size: @max_size,
      ttl: @ttl,
      utilization: (@cache.size.to_f / @max_size * 100).round(2)
    }
  end
end