Class: LRU::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/lru/cache.rb,
lib/lru/cache/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max, hash = {}) ⇒ Cache

Returns a new instance of Cache.



14
15
16
17
# File 'lib/lru/cache.rb', line 14

def initialize(max, hash={})
  @max = max
  @content = hash
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/lru/cache.rb', line 12

def content
  @content
end

#maxObject (readonly)

Returns the value of attribute max.



12
13
14
# File 'lib/lru/cache.rb', line 12

def max
  @max
end

Instance Method Details

#[](key) ⇒ Object Also known as: get



19
20
21
# File 'lib/lru/cache.rb', line 19

def [](key)
  content[key] = content.delete(key) if content.has_key? key
end

#[]=(key, value) ⇒ Object Also known as: set



24
25
26
27
28
# File 'lib/lru/cache.rb', line 24

def []=(key, value)
  content.delete key if content.has_key? key
  content.delete(content.first.first) if content.length == max
  content[key] = value
end