这将是我的建议,简单有效:
module Foo =
[...]
let fcache, foo' = memoize foo
module Bar =
[...]
let bcache, bar' = memoize bar
module Main =
open Foo
open Bar
let clearCaches = [
fcache.Clear
bcache.Clear
]
[...]
let result =
foobar (foo' a) (bar' b)
let clearAll() =
clearCaches |> Seq.iter (fun clear -> clear())
更新
如果你想收集
clear
函数自动memoize函数可以执行此操作,如下所示:
let clearCaches = Dictionary<_,_>()
let memoize (name:string) f =
let cache = new ConcurrentDictionary<'key,'value>()
clearCaches.Add(name, cache.Clear)
fun x -> cache.GetOrAdd(x, Func<'key, 'value>f)
module Foo =
[...]
let foo' = memoize "Foo.foo" foo
module Bar =
[...]
let bar' = memoize "Bar.bar" bar
module Main =
open Foo
open Bar
[...]
let result =
foobar (foo' a) (bar' b)
let clearAll() =
clearCaches |> Seq.iter (fun kvp -> kvp.Value())
这也将允许您单独清除它们或使用某些条件,如按模块等。