class Proxy::OpenBolt::LruCache

Thread-safe, size-bounded cache that evicts the least recently used entry when full. Built on Ruby's insertion-ordered Hash. Implemented here rather than pulling in a gem to avoid adding packaging dependencies for something this straightforward.

Public Class Methods

new(max_size) click to toggle source
# File lib/smart_proxy_openbolt/lru_cache.rb, line 7
def initialize(max_size)
  raise ArgumentError, 'max_size must be at least 1' if max_size < 1
  @max_size = max_size
  @hash = {}
  @mutex = Mutex.new
end

Public Instance Methods

delete(key) click to toggle source
# File lib/smart_proxy_openbolt/lru_cache.rb, line 31
def delete(key)
  @mutex.synchronize { @hash.delete(key) }
end
get(key) click to toggle source
# File lib/smart_proxy_openbolt/lru_cache.rb, line 14
def get(key)
  @mutex.synchronize do
    return nil unless @hash.key?(key)
    # Move to end (most recently used)
    value = @hash.delete(key)
    @hash[key] = value
  end
end
key?(key) click to toggle source
# File lib/smart_proxy_openbolt/lru_cache.rb, line 35
def key?(key)
  @mutex.synchronize { @hash.key?(key) }
end
put(key, value) click to toggle source
# File lib/smart_proxy_openbolt/lru_cache.rb, line 23
def put(key, value)
  @mutex.synchronize do
    @hash.delete(key) if @hash.key?(key)
    @hash[key] = value
    @hash.shift if @hash.size > @max_size
  end
end
size() click to toggle source
# File lib/smart_proxy_openbolt/lru_cache.rb, line 39
def size
  @mutex.synchronize { @hash.size }
end