class MetalArchives::LRUCache

Generic LRU memory cache

Public Class Methods

new(size = 100) click to toggle source
# File lib/metal_archives/utils/lru_cache.rb, line 7
def initialize(size = 100)
  @size = size

  # Underlying data store
  @cache = {}

  # Array of keys in order of insertion
  @keys = []
end

Public Instance Methods

[](key) click to toggle source
# File lib/metal_archives/utils/lru_cache.rb, line 27
def [](key)
  if @keys.include? key
    MetalArchives.config.logger.debug "Cache hit for #{key}"
    @keys.delete key
    @keys << key
  else
    MetalArchives.config.logger.debug "Cache miss for #{key}"
  end

  @cache[key]
end
[]=(key, value) click to toggle source
# File lib/metal_archives/utils/lru_cache.rb, line 17
def []=(key, value)
  @cache[key] = value

  @keys.delete key if @keys.include? key

  @keys << key

  pop if @keys.size > @size
end
clear() click to toggle source
# File lib/metal_archives/utils/lru_cache.rb, line 39
def clear
  @cache.clear
  @keys.clear
end
delete(key) click to toggle source
# File lib/metal_archives/utils/lru_cache.rb, line 48
def delete(key)
  @cache.delete key
end
include?(key) click to toggle source
# File lib/metal_archives/utils/lru_cache.rb, line 44
def include?(key)
  @cache.include? key
end