Fix Common implementation.

toying-with-ramdb
Philippe PITTOLI 2024-05-28 03:28:40 +02:00
parent 279f4379e8
commit c105b7fa33
1 changed files with 9 additions and 2 deletions

View File

@ -33,7 +33,7 @@
#
# NOTE: fast for frequently requested data and requires a stable (and configurable) amount of memory.
class DODB::Storage::Common(V) < DODB::Storage::Cached(V)
# The *fifo* an `EfficientFIFO` instance where the key of the requested data is pushed.
# The *fifo* is an instance of `EfficientFIFO` where the key of the requested data is pushed.
# In case the number of stored entries exceeds what is allowed, the least recently used entry is removed.
property fifo : EfficientFIFO(Int32)
@ -50,8 +50,15 @@ class DODB::Storage::Common(V) < DODB::Storage::Cached(V)
end
end
# Verifies that the value is in cache, or read it on disk.
# Pushes the key in the fifo.
def [](key : Int32) : V
val = @data[key] rescue raise MissingEntry.new(key)
val = @data[key]?
if val.nil?
raise MissingEntry.new(key) unless ::File.exists? file_path key
val = read file_path key
@data[key] = val
end
push_fifo key
val
end