class Tins::LRUCache

Attributes

capacity[R]

Public Class Methods

new(capacity) click to toggle source
# File lib/tins/lru_cache.rb, line 15
def initialize(capacity)
  @capacity = capacity
  @data     = {}
end

Public Instance Methods

[](key) click to toggle source
# File lib/tins/lru_cache.rb, line 22
def [](key)
  case value = @data.delete(key){ not_exist }
  when not_exist
    nil
  else
    @data[key] = value
  end
end
[]=(key, value) click to toggle source
# File lib/tins/lru_cache.rb, line 31
def []=(key, value)
  @data.delete(key)
  @data[key] = value
  if @data.size > @capacity
    @data.delete(@data.keys.first)
  end
  value
end
clear() click to toggle source
# File lib/tins/lru_cache.rb, line 48
def clear
  @data.clear
end
delete(key) click to toggle source
# File lib/tins/lru_cache.rb, line 44
def delete(key)
  @data.delete(key)
end
each(&block) click to toggle source
# File lib/tins/lru_cache.rb, line 40
def each(&block)
  @data.reverse_each(&block)
end
size() click to toggle source
# File lib/tins/lru_cache.rb, line 52
def size
  @data.size
end

Private Instance Methods

not_exist() click to toggle source
# File lib/tins/lru_cache.rb, line 58
def not_exist
  self.class.send(:not_exist)
end