class MetalArchives::Cache::Memory

Generic LRU memory cache

Public Instance Methods

[](key) click to toggle source
# File lib/metal_archives/cache/memory.rb, line 14
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/cache/memory.rb, line 26
def []=(key, value)
  cache[key] = value

  keys.delete key if keys.include? key

  keys << key

  pop if keys.size > options[:size]
end
clear() click to toggle source
# File lib/metal_archives/cache/memory.rb, line 36
def clear
  cache.clear
  keys.clear
end
delete(key) click to toggle source
# File lib/metal_archives/cache/memory.rb, line 45
def delete(key)
  cache.delete key
end
include?(key) click to toggle source
# File lib/metal_archives/cache/memory.rb, line 41
def include?(key)
  cache.include? key
end
validate!() click to toggle source
# File lib/metal_archives/cache/memory.rb, line 9
def validate!
  raise Errors::InvalidConfigurationError, "size has not been configured" if options[:size].blank?
  raise Errors::InvalidConfigurationError, "size must be a number" unless options[:size].is_a? Integer
end

Private Instance Methods

cache() click to toggle source
# File lib/metal_archives/cache/memory.rb, line 51
def cache
  # Underlying data store
  @cache ||= {}
end
keys() click to toggle source
# File lib/metal_archives/cache/memory.rb, line 56
def keys
  # Array of keys in order of insertion
  @keys ||= []
end
pop() click to toggle source
# File lib/metal_archives/cache/memory.rb, line 61
def pop
  to_remove = keys.shift(keys.size - options[:size])

  to_remove.each { |key| cache.delete key }
end