Class: Oga::LRU Private

Inherits:
Object
  • Object
show all
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]

API:

  • private

Instance Method Summary collapse

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.

Parameters:

  • (defaults to: 1024)

API:

  • private



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.

Parameters:

Returns:

API:

  • private



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.

Parameters:

API:

  • private



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

#clearObject

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.

API:

  • private



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.

Parameters:

Returns:

API:

  • private



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.

Parameters:

Returns:

API:

  • private



89
90
91
# File 'lib/oga/lru.rb', line 89

def key?(key)
  synchronize { @cache.key?(key) }
end

#keysArray

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:

API:

  • private



83
84
85
# File 'lib/oga/lru.rb', line 83

def keys
  synchronize { @keys }
end

#maximumFixnum

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:

API:

  • private



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.

Parameters:

API:

  • private



35
36
37
38
39
40
41
# File 'lib/oga/lru.rb', line 35

def maximum=(value)
  synchronize do
    @maximum = value

    resize
  end
end

#sizeFixnum 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.

Returns:

API:

  • private



102
103
104
# File 'lib/oga/lru.rb', line 102

def size
  synchronize { @cache.size }
end