Request: Add async `Util.Cache()` method
The Util.Cache()
method is synchronous, including the dataFetcher
factory function.
Could an asynchronous version be added?
Current workaround is to cache an unpopulated serializable object (e.g., dictionary) and populate later.
Comments
An async cache is normally implemented as a synchronous cache of tasks. For example:
I'm not sure what an asynchronous version of Util.Cache would accomplish. Can you explain?
Ah, didn't think to have it return
Task<T>
. That'll work.After using this more, I don't think caching the task is sufficient. If an exception occurs in the task, the failure is cached and it gets awkward trying to retry the call and replace the result. Had to restructure it completely.
For specialized scenarios, you can create your own caching methods in My Extensions. If the dictionary is stored in a static field, it will persist between executions.
To illustrate a specialized scenario, here's the cache that LINQPad uses internally to cache NuGet package vulnerabilities. It periodically refreshes, can be saved to disk, and in the case of a fault, uses the previously cached value.
The LINQPad 8.1 already have a
Util.CacheAsync
method now.That's right - new
Util.CacheAsync
method ensures that faulted tasks are not cached.