diff --git a/src/dodb/storage/common.cr b/src/dodb/storage/common.cr index 0857967..744a25d 100644 --- a/src/dodb/storage/common.cr +++ b/src/dodb/storage/common.cr @@ -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