Class: Oga::LRU Private
- Inherits:
-
Object
- Object
- Oga::LRU
- Defined in:
- lib/oga/lru.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Thread-safe LRU cache using a Hash as the underlying storage engine. Whenever the size of the cache exceeds the given limit the oldest keys are removed (base on insert order).
This class uses its own list of keys (as returned by #keys) instead of
relying on Hash#keys
as the latter allocates a new Array upon every call.
This class doesn’t use MonitorMixin due to the extra overhead it adds compared to using a Mutex directly.
Example usage:
cache = LRU.new(3)
cache[:a] = 10
cache[:b] = 20
cache[:c] = 30
cache[:d] = 40
cache.keys # => [:b, :c, :d]
Instance Method Summary collapse
-
#[](key) ⇒ Mixed
private
Returns the value of the key.
-
#[]=(key, value) ⇒ Object
private
Sets the key and its value.
-
#clear ⇒ Object
private
Removes all keys from the cache.
-
#get_or_set(key) ⇒ Mixed
private
Returns a key if it exists, otherwise yields the supplied block and uses its return value as the key value.
-
#initialize(maximum = 1024) ⇒ LRU
constructor
private
A new instance of LRU.
-
#key?(key) ⇒ TrueClass|FalseClass
private
-
#keys ⇒ Array
private
-
#maximum ⇒ Fixnum
private
-
#maximum=(value) ⇒ Object
private
-
#size ⇒ Fixnum
(also: #length)
private
Constructor Details
#initialize(maximum = 1024) ⇒ LRU
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of LRU
26 27 28 29 30 31 32 |
# File 'lib/oga/lru.rb', line 26 def initialize(maximum = 1024) @maximum = maximum @cache = {} @keys = [] @mutex = Mutex.new @owner = Thread.current end |
Instance Method Details
#[](key) ⇒ Mixed
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the value of the key.
52 53 54 |
# File 'lib/oga/lru.rb', line 52 def [](key) synchronize { @cache[key] } end |
#[]=(key, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the key and its value. Old keys are discarded if the LRU size exceeds the limit.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/oga/lru.rb', line 61 def []=(key, value) synchronize do @cache[key] = value @keys.delete(key) if @keys.include?(key) @keys << key resize end end |
#clear ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes all keys from the cache.
94 95 96 97 98 99 |
# File 'lib/oga/lru.rb', line 94 def clear synchronize do @keys.clear @cache.clear end end |
#get_or_set(key) ⇒ Mixed
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a key if it exists, otherwise yields the supplied block and uses its return value as the key value.
78 79 80 |
# File 'lib/oga/lru.rb', line 78 def get_or_set(key) synchronize { self[key] ||= yield } end |
#key?(key) ⇒ TrueClass|FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
89 90 91 |
# File 'lib/oga/lru.rb', line 89 def key?(key) synchronize { @cache.key?(key) } end |
#keys ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
83 84 85 |
# File 'lib/oga/lru.rb', line 83 def keys synchronize { @keys } end |
#maximum ⇒ Fixnum
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
44 45 46 |
# File 'lib/oga/lru.rb', line 44 def maximum synchronize { @maximum } end |
#maximum=(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
35 36 37 38 39 40 41 |
# File 'lib/oga/lru.rb', line 35 def maximum=(value) synchronize do @maximum = value resize end end |
#size ⇒ Fixnum Also known as: length
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
102 103 104 |
# File 'lib/oga/lru.rb', line 102 def size synchronize { @cache.size } end |