Class: Cache::LRU

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/background_queue/server_lib/lru.rb

Overview

Defines a local LRU cache. All operations are maintained in constant size regardless of the size of the Cache. When creating the cache, you specify the max size of the cache.

Example:

cache = Cache::LRU.new(:max_elements => 5)
 cache.put(:a, 1)
 cache[:a] = 2
 cache.get(:b) { 1 }
 cache[:b]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ LRU

opts:

- max_elements - maximum number of elements to keep in the
  cache at any time. The default is 100 elements


42
43
44
45
46
47
48
49
# File 'lib/background_queue/server_lib/lru.rb', line 42

def initialize(opts = {})
  opts = { :max_elements => 100 }.merge(opts)
  @max_elements = opts.delete(:max_elements)
  raise "Invalid options: #{opts.keys.join(' ')}" if opts.keys.size > 0
  @keys = LinkedList.new
  @map = {}
  @size = 0
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



38
39
40
# File 'lib/background_queue/server_lib/lru.rb', line 38

def keys
  @keys
end

#sizeObject (readonly)

Returns the value of attribute size.



37
38
39
# File 'lib/background_queue/server_lib/lru.rb', line 37

def size
  @size
end

Instance Method Details

#[](key) ⇒ Object



64
65
66
# File 'lib/background_queue/server_lib/lru.rb', line 64

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



81
82
83
# File 'lib/background_queue/server_lib/lru.rb', line 81

def []=(key, value)
  put(key, value)
end

#clear!Object



51
52
53
# File 'lib/background_queue/server_lib/lru.rb', line 51

def clear!
  initialize( :max_elements => @max_elements )
end

#delete(key) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/background_queue/server_lib/lru.rb', line 103

def delete(key) 
  if el = @map[key]
    delete_element(el)
    @size -= 1
  else
    nil
  end
end

#eachObject

Iterates through all of the key/value pairs added to the cache, in random order. Accepts a block that is yielded to with the key and value for each entry in the cache.



58
59
60
61
62
# File 'lib/background_queue/server_lib/lru.rb', line 58

def each
  @map.each do |k, el| 
    yield k, el.value
  end
end

#get(key) ⇒ Object

Fetches the value of the element with the given key. If this key does not exist in the cache, you can provide an optional code block that we’ll yield to to repopulate the value



71
72
73
74
75
76
77
78
79
# File 'lib/background_queue/server_lib/lru.rb', line 71

def get(key)
  if el = @map[key]
    @keys.move_to_head(el)
    return el.value
  elsif block_given?
    return put(key, yield)
  end
  return nil
end

#put(key, value) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/background_queue/server_lib/lru.rb', line 85

def put(key, value)
  el = @map[key]
  if el
    el.value = value
    @keys.move_to_head(el)
  else
    el = @keys.add(key, value)
    @size += 1
  end
  @map[key] = el

  if @size > @max_elements
    delete_element(@keys.last) 
    @size -= 1
  end
  value
end