def initialize(@capacity : Int32)
@storage = Immutable::Map(K, V).new
end
def fetch(key : K, &block : K -> V) : V
value = storage[key]?
return value if value
value = yield key
unless full?
_storage = storage.set key, value
self.storage = _storage
end
value
end
def full?
storage.size >= capacity
end
Summary
Thread
for some time. (I.e.-Dpreview_mt
)Hash
is not thread-safe,Hash
is volatile.Solution
Improve
module MaxMindDB struct Cache(K, V) property capacity : Int32 property storage : Immutable::Map(K, V)
end end
References