Home
Insert
Update snippet 'Memoize Async Function'
Title
Description
Cache a function's asynchronously-computed result for each argument to reduce expensive and repetitive computation of an asynchronous operation. Uses a concurrent dictionary for backing storage, and at-least-once invocation semantics per key.
Source code
let inline memoize f = let dict = System.Collections.Concurrent.ConcurrentDictionary() fun x -> async { match dict.TryGetValue x with | true, result -> return result | false, _ -> let! result = f x dict.TryAdd(x, result) |> ignore return result }
Tags
memoize async concurrent function
memoize async concurrent function
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update