This week's blog series was on implementing a cache map. This is a data structure that implements the same interface as a map but it has some extra behavior that a normal map does not normally entail.
The first is that if a value is not present then it doesn't result in the returning of a value like `null` or the throwing of an exception indicating the failure. Rather, a specific function (of the user's choosing) is called with the key and it will return this value. It does, however, store this value into the map's internal data structures such that successive accesses will return it without invoking this "getter" function.
Now, the reason it is called a c a c h e map is because this value has a lifetime associated with it and, one it expires, will cause the entry's removal from the map. Meaning that a successive keying-into the map will invoke the "getter" function when called after the lifetime has elapsed. This lifetime is also a configurable parameter that the user can choose.
This was quite a fun one to implement it as I actually needed it in some other project of mine hence it felt good to implement something with a good reason.
⚡️ Post:
https://deavmi.assigned.network/blog/niknaks_cachemap/
#dlang #algos