class MetalArchives::LRUCache

Generic LRU memory cache

Public Class Methods

new(size = 100) click to toggle source
# File lib/metal_archives/lru_cache.rb, line 8
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/lru_cache.rb, line 28
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/lru_cache.rb, line 18
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/lru_cache.rb, line 40
def clear
  @cache.clear
  @keys.clear
end
delete(key) click to toggle source
# File lib/metal_archives/lru_cache.rb, line 49
def delete(key)
  @cache.delete key
end
include?(key) click to toggle source
# File lib/metal_archives/lru_cache.rb, line 45
def include?(key)
  @cache.include? key
end