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]

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:

  • maximum (Fixnum) (defaults to: 1024)


30
31
32
33
34
35
36
# File 'lib/oga/lru.rb', line 30

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:

  • key (Mixed)

Returns:

  • (Mixed)


62
63
64
# File 'lib/oga/lru.rb', line 62

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:

  • key (Mixed)
  • value (Mixed)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/oga/lru.rb', line 73

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.



114
115
116
117
118
119
# File 'lib/oga/lru.rb', line 114

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:

  • key (Mixed)

Returns:

  • (Mixed)


92
93
94
# File 'lib/oga/lru.rb', line 92

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:

  • key (Mixed)

Returns:

  • (TrueClass|FalseClass)


107
108
109
# File 'lib/oga/lru.rb', line 107

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:

  • (Array)


99
100
101
# File 'lib/oga/lru.rb', line 99

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:

  • (Fixnum)


52
53
54
# File 'lib/oga/lru.rb', line 52

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:

  • value (Fixnum)


41
42
43
44
45
46
47
# File 'lib/oga/lru.rb', line 41

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:

  • (Fixnum)


124
125
126
# File 'lib/oga/lru.rb', line 124

def size
  synchronize { @cache.size }
end