Class: Fmt::LRUCache

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/fmt/lru_cache.rb

Overview

A threadsafe fixed-size LRU in-memory cache Grows to capacity then evicts the least used entries

Examples:

cache = Fmt::Cache.new

cache.put :key, "value"
cache.get :key
cache.delete :key
cache.fetch :key, "default"
cache.fetch(:key) { "default" }

Capacity

Fmt::Cache.capacity = 10_000

Constant Summary collapse

DEFAULT_CAPACITY =

: Integer – default capacity

5_000

Instance Method Summary collapse

Constructor Details

#initialize(capacity: DEFAULT_CAPACITY) ⇒ LRUCache

Constructor



28
29
30
31
32
# File 'lib/fmt/lru_cache.rb', line 28

def initialize(capacity: DEFAULT_CAPACITY)
  super()
  @capacity = capacity
  @store = {}
end

Instance Method Details

#capacityObject

The cache max capacity (number of entries)



36
37
38
# File 'lib/fmt/lru_cache.rb', line 36

def capacity
  synchronize { @capacity }
end

#capacity=(capacity) ⇒ Object

Set the max capacity (number of entries)



43
44
45
# File 'lib/fmt/lru_cache.rb', line 43

def capacity=(capacity)
  synchronize { @capacity = capacity.to_i }
end

#capped?Boolean

Indicates if the cache is capped



49
50
51
# File 'lib/fmt/lru_cache.rb', line 49

def capped?
  synchronize { capacity >= 0 }
end

#clearObject

Clears the cache



55
56
57
# File 'lib/fmt/lru_cache.rb', line 55

def clear
  synchronize { store.clear }
end

#delete(key) ⇒ Object

Deletes the entry for the specified key



62
63
64
# File 'lib/fmt/lru_cache.rb', line 62

def delete(key)
  synchronize { store.delete key }
end

#fetch(key, default = nil, &block) ⇒ Object

Fetches the value for the specified key Writes the default value if the key is not found



72
73
74
75
76
# File 'lib/fmt/lru_cache.rb', line 72

def fetch(key, default = nil, &block)
  return get(key) if key?(key)
  default ||= block&.call
  synchronize { put key, default }
end

#fetch_unsafe(key, default = nil, &block) ⇒ Object

Fetches a value from the cache without synchronization (not thread safe)



83
84
85
86
# File 'lib/fmt/lru_cache.rb', line 83

def fetch_unsafe(key, default = nil, &block)
  return store[key] if store.key?(key)
  store[key] = (default || block&.call)
end

#full?Boolean

Indicates if the cache is full



90
91
92
# File 'lib/fmt/lru_cache.rb', line 90

def full?
  synchronize { capped? && store.size > capacity }
end

#get(key) ⇒ Object Also known as: []

Retrieves the value for the specified key



96
97
98
99
100
101
# File 'lib/fmt/lru_cache.rb', line 96

def get(key)
  synchronize do
    reposition(key) if key?(key)
    store[key]
  end
end

#key?(key) ⇒ Boolean

Indicates if the cache contains the specified key



112
113
114
# File 'lib/fmt/lru_cache.rb', line 112

def key?(key)
  synchronize { store.key? key }
end

#keysObject

Cache keys



105
106
107
# File 'lib/fmt/lru_cache.rb', line 105

def keys
  synchronize { store.keys }
end

#lock(&block) ⇒ Object

Executes a block with a synchronized mutex



162
163
164
# File 'lib/fmt/lru_cache.rb', line 162

def lock(&block)
  synchronize(&block)
end

#put(key, value) ⇒ Object Also known as: []=

Stores the value for the specified key



120
121
122
123
124
125
126
127
# File 'lib/fmt/lru_cache.rb', line 120

def put(key, value)
  synchronize do
    delete key if capped? # keep keey fresh if capped
    store[key] = value
    store.shift if full? # resize the cache if necessary
    value
  end
end

#reset_capacityObject

Resets the cache capacity to the default



131
132
133
# File 'lib/fmt/lru_cache.rb', line 131

def reset_capacity
  synchronize { @capacity = DEFAULT_CAPACITY }
end

#sizeObject

The current size of the cache (number of entries)



137
138
139
# File 'lib/fmt/lru_cache.rb', line 137

def size
  synchronize { store.size }
end

#slice(*keys) ⇒ Object

Returns a Hash with only the given keys



144
145
146
# File 'lib/fmt/lru_cache.rb', line 144

def slice(*keys)
  synchronize { store.slice(*keys) }
end

#to_hObject

Hash representation of the cache



150
151
152
# File 'lib/fmt/lru_cache.rb', line 150

def to_h
  synchronize { store.dup }
end

#valuesObject

Cache values



156
157
158
# File 'lib/fmt/lru_cache.rb', line 156

def values
  synchronize { store.values }
end