Class: LookupBy::Caching::LRU

Inherits:
Hash
  • Object
show all
Defined in:
lib/lookup_by/caching/lru.rb

Direct Known Subclasses

SafeLRU

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maxsize) ⇒ LRU

Returns a new instance of LRU.



6
7
8
9
10
11
# File 'lib/lookup_by/caching/lru.rb', line 6

def initialize(maxsize)
  super()

  @maxsize = maxsize
  @lru     = []
end

Instance Attribute Details

#lruObject (readonly)

Returns the value of attribute lru.



4
5
6
# File 'lib/lookup_by/caching/lru.rb', line 4

def lru
  @lru
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
22
23
# File 'lib/lookup_by/caching/lru.rb', line 19

def [](key)
  return nil unless has_key?(key)
  touch(key)
  super
end

#[]=(key, value) ⇒ Object



25
26
27
28
29
# File 'lib/lookup_by/caching/lru.rb', line 25

def []=(key, value)
  touch(key)
  super
  prune
end

#clearObject



13
14
15
16
17
# File 'lib/lookup_by/caching/lru.rb', line 13

def clear
  @lru.clear

  super
end

#delete(key) ⇒ Object



35
36
37
38
39
# File 'lib/lookup_by/caching/lru.rb', line 35

def delete(key)
  @lru.delete(key)

  super
end

#merge!(hash) ⇒ Object



31
32
33
# File 'lib/lookup_by/caching/lru.rb', line 31

def merge!(hash)
  hash.each { |k, v| self[k] = v }
end

#to_hObject



41
42
43
# File 'lib/lookup_by/caching/lru.rb', line 41

def to_h
  {}.merge!(self)
end