Does Util.Cache() reliably cache the results of asynchronous functions?
Title. With this test, it does seem to work, but should I expect it to?
async Task Main() { $"Running now: {DateTime.Now}".Dump(); var date = await Util.Cache(async () => await GetDate(), "now"); date.Dump(); async Task<DateTime> GetDate() { await Task.Delay(3000); return DateTime.Now; } }
Comments
I don't see why not. You're caching a task, so on subsequent runs, Util.Cache will return the same (completed) task.
Ok great. I was under the impression that all objects needed to be serializable and since tasks weren't, I wasn't sure it would/should work.
The serializable restriction applies to types that you've defined in the script itself, when you make changes to the script. For other types, it can re-use the existing object.