Class: Tins::LRUCache

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/tins/lru_cache.rb

Overview

An LRU (Least Recently Used) cache implementation.

This cache maintains a fixed-size collection of key-value pairs, automatically removing the least recently accessed item when the capacity is exceeded. Both read and write are considered access.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ LRUCache

Initializes a new LRU cache with the specified capacity.

Parameters:

  • capacity (Integer)

    maximum number of items the cache can hold



13
14
15
16
17
18
# File 'lib/tins/lru_cache.rb', line 13

def initialize(capacity)
  @capacity = Integer(capacity)
  @capacity >= 1 or
    raise ArgumentError, "capacity should be >= 1, was #@capacity"
  @data     = {} # Least-recently used will always be the first element
end

Instance Attribute Details

#capacityInteger (readonly)

Returns the maximum capacity of the cache.

Returns:

  • (Integer)

    the cache capacity



23
24
25
# File 'lib/tins/lru_cache.rb', line 23

def capacity
  @capacity
end

Instance Method Details

#[](key) ⇒ Object?

Retrieves the value associated with the given key.

If the key exists, it is moved to the most recently used position. Returns nil if the key does not exist.

Parameters:

  • key (Object)

    the key to look up

Returns:

  • (Object, nil)

    the value for the key or nil if not found



32
33
34
35
36
# File 'lib/tins/lru_cache.rb', line 32

def [](key)
  if @data.has_key?(key)
    @data[key] = @data.delete(key)
  end
end

#[]=(key, value) ⇒ Object

Associates a value with a key in the cache.

If the key already exists, its position is updated to most recently used. If adding this item exceeds the capacity, the least recently used item is removed.

Parameters:

  • key (Object)

    the key to set

  • value (Object)

    the value to associate with the key

Returns:

  • (Object)

    the assigned value



47
48
49
50
51
52
53
54
# File 'lib/tins/lru_cache.rb', line 47

def []=(key, value)
  @data.delete(key)
  @data[key] = value
  if @data.size > @capacity
    @data.shift
  end
  value
end

#clearTins::LRUCache

Removes all items from the cache.

Returns:



79
80
81
82
# File 'lib/tins/lru_cache.rb', line 79

def clear
  @data.clear
  self
end

#delete(key) ⇒ Object?

Removes and returns the value associated with the given key.

Parameters:

  • key (Object)

    the key to delete

Returns:

  • (Object, nil)

    the removed value or nil if not found



72
73
74
# File 'lib/tins/lru_cache.rb', line 72

def delete(key)
  @data.delete(key)
end

#each {|key, value| ... } ⇒ Enumerator

Iterates over all key-value pairs in the cache.

Items are yielded in order from most recently used to least recently used.

Yields:

  • (key, value)

    yields each key-value pair

Yield Parameters:

Returns:

  • (Enumerator)

    if no block is given



64
65
66
# File 'lib/tins/lru_cache.rb', line 64

def each(&block)
  @data.reverse_each(&block)
end

#sizeInteger

Returns the number of items currently in the cache.

Returns:

  • (Integer)

    the current size of the cache



87
88
89
# File 'lib/tins/lru_cache.rb', line 87

def size
  @data.size
end