Does a LRU cache that prevents double work exist?

Doing a quick search I have found several libraries reimplementing NSCache, but haven't found a single one where the get function also takes in a block to calculate the value and then either calls that block inside a task and saves the task to the cache or awaits the already existing task.

I don't think it even needs any other public functions, just init and get.

Let's build the first one, and be heroes! https://www.youtube.com/watch?v=vDVo1TIhwRk&t=120s

This is what I put together: Cyberbeni/LruCache: Asynchronous Swift LRU cache that prevents double work. - Codeberg.org

Haven't used it in a project yet but the tests should cover the cases of how people might use it. Was thinking of making it a ~Copyable struct but that prevents people from directly using it (for example DelayedCacheItemTests.basicUsage() would require you to wrap it in a class yourself).

1 Like