class Gapic::LruHash
access/creation of the keys.
Linked list based hash maintaining the order of
@private
#
def get key
def get key return nil unless @cache.key? key node = @cache[key] move_to_top node node.value end
def initialize size = 1
def initialize size = 1 raise ArgumentError, "The size of LRU hash can't be < 1" unless size > 1 @start = nil @end = nil @size = size @cache = {} end
def insert_at_top node
def insert_at_top node if @start.nil? @start = node @end = node else node.next = @start @start.prev = node @start = node end end
def move_to_top node
def move_to_top node return if node.equal? @start if node.equal? @end @end = node.prev @end.next = nil else node.prev.next = node.next node.next.prev = node.prev end node.prev = nil node.next = @start @start.prev = node @start = node end
def put key, value
def put key, value if @cache.key? key node = @cache[key] node.value = value move_to_top node else remove_tail if @cache.size >= @size new_node = Node.new key, value insert_at_top new_node @cache[key] = new_node end end
def remove_tail
def remove_tail @cache.delete @end.key @end = @end.prev @end.next = nil if @end end