class LRUCacher

Constants

VERSION

Public Class Methods

new() click to toggle source
# File lib/lru-cacher.rb, line 4
def initialize
  @table = {}
  @head  = nil
  @tail  = nil
end

Public Instance Methods

delete(key) click to toggle source
# File lib/lru-cacher.rb, line 56
def delete(key)
  current_node = @table[key]
  if current_node
    if @head == current_node && @tail == current_node
      @head, @tail = nil
    elsif @head == current_node
      @head           = current_node.next_node
      @head.prev_node = nil
    elsif @tail == current_node
      @tail           = current_node.prev_node
      @tail.next_node = nil
    else
      current_node.prev_node.next_node = current_node.next_node
      current_node.next_node.prev_node = current_node.prev_node
    end
    @table.delete(key)
  end
end
exists?(key) click to toggle source
# File lib/lru-cacher.rb, line 24
def exists?(key)
  @table.key?(key)
end
get(key) click to toggle source
# File lib/lru-cacher.rb, line 32
def get(key)
  current_node = @table[key]
  if current_node
    if @tail == current_node
      current_node
    elsif @head == current_node
      @head                  = current_node.next_node
      @head.prev_node        = nil
      current_node.prev_node = @tail
      current_node.next_node = nil
      @tail.next_node        = current_node
      @tail                  = current_node
    else
      current_node.prev_node.next_node = current_node.next_node
      current_node.next_node.prev_node = current_node.prev_node
      current_node.prev_node           = @tail
      current_node.next_node           = nil
      @tail.next_node                  = current_node
      @tail                            = current_node
    end
  end
  current_node
end
set(key, value) click to toggle source
# File lib/lru-cacher.rb, line 10
def set(key, value)
  if exists?(key)
    @table[key].value = value
    current_node      = @table[key]
  else
    current_node = LRUCacher::Node.new(value, key, @tail, nil)
  end
  @head           = current_node unless @tail
  @tail.next_node = current_node if @tail
  @tail           = current_node
  @table[key]     = current_node
  delete(@head.key) if @head && over_threshold?
end
size() click to toggle source
# File lib/lru-cacher.rb, line 28
def size
  @table.size
end