懒惰..但在F#中热切的数据加载器

时间:2012-09-16 17:33:54

标签: f# lazy-loading eager-loading agent mailboxprocessor

有没有人知道关于以下主题的'现有技术':

  • 我的数据需要花费一些时间才能加载。它们是各种股票的历史水平。
  • 我想以某种方式预加载它们,以避免使用我的应用程序时的延迟
  • 然而,在开始时将它们预先加载到一个块中会使我的应用程序首先无法响应,这不是用户友好的

所以我想加载我的数据.... 除非用户没有请求任何并且玩他已经拥有的东西,在这种情况下我会喜欢一点一点地得到。所以它既不“懒惰”也不“渴望”,更“懒惰,当你需要”和“渴望你可以”时,因此缩写为LWYNEWYC。

我已经做了以下似乎有效,但我只是想知道是否有一种公认的,有福的方法吗?

let r = LoggingFakeRepo () :> IQuoteRepository
r.getHisto "1" |> ignore  //prints Getting histo for 1 when called

let rc =  RepoCached (r) :> IQuoteRepository
rc.getHisto "1" |> ignore //prints Getting histo for 1 the first time only

let rcc =  RepoCachedEager (r) :> IQuoteRepository
rcc.getHisto "100" |> ignore  //prints Getting histo 1..100 by itself BUT
                              //prints Getting histo 100 immediately when called

和班级

type IQuoteRepository = 
   abstract getUnderlyings : string seq
   abstract getHisto :  string -> string

type LoggingFakeRepo () =
   interface IQuoteRepository with 
      member x.getUnderlyings = printfn "getting underlyings"
                                [1 .. 100] |> List.map string :> _

      member x.getHisto udl = printfn "getting histo for %A" udl
                              "I am a historical dataset in a disguised party"

type RepoCached (rep : IQuoteRepository) =
   let memoize f =
     let cache = new System.Collections.Generic.Dictionary<_, _>()
     fun x ->
        if cache.ContainsKey(x) then cache.[x]
        else let res = f x
             cache.[x] <- res
             res
   let udls = lazy (rep.getUnderlyings )
   let gethistom = memoize rep.getHisto

   interface IQuoteRepository with 
      member x.getUnderlyings = udls.Force()
      member x.getHisto udl = gethistom udl

type Message = string * AsyncReplyChannel<UnderlyingWrap>
type RepoCachedEager (rep : IQuoteRepository) =
   let udls = rep.getUnderlyings

   let agent = MailboxProcessor<Message>.Start(fun inbox ->
      let repocached = RepoCached (rep) :> IQuoteRepository
      let rec loop l =
         async {  try
                     let timeout = if l|> List.isEmpty  then -1 else 50
                     let! (udl, replyChannel) = inbox.Receive(timeout)
                     replyChannel.Reply(repocached.getHisto udl)
                     do! loop l
                  with 
                  | :? System.TimeoutException -> 
                     let udl::xs = l
                     repocached.getHisto udl |> ignore
                     do! loop xs
          }
      loop (udls |> Seq.toList))

   interface IQuoteRepository with 
      member x.getUnderlyings = udls
      member x.getHisto udl = agent.PostAndReply(fun reply -> udl, reply)

1 个答案:

答案 0 :(得分:4)

我喜欢你的解决方案。我认为使用代理来实现一些带有超时的后台加载是一个很好的方法 - 代理可以很好地封装可变状态,所以它显然是安全的,你可以很容易地编码你想要的行为。

我认为asynchronous sequences可能是另一个有用的抽象(如果我是正确的,它们现在可以在FSharpX中使用)。异步序列表示异步生成更多值的计算,因此它们可能是将数据加载器与其余代码分离的好方法。

我认为您在某些时候仍然需要代理进行同步,但您可以使用异步序列很好地分离不同的问题。

加载数据的代码可能如下所示:

let loadStockPrices repo = asyncSeq {
  // TODO: Not sure how you detect that the repository has no more data...
  while true do
    // Get next item from the repository, preferably asynchronously!
    let! data = repo.AsyncGetNextHistoricalValue()
    // Return the value to the caller...
    yield data }

此代码表示数据加载器,它将其与使用它的代码分开。从使用数据源的代理程序中,您可以使用AsyncSeq.iterAsync来使用这些值并对它们执行某些操作。

使用iterAsync,您指定为使用者的功能是异步。它可能会阻塞(即使用Sleep),当它阻塞时,源 - 即你的加载器 - 也被阻止。这是从消耗数据的代码控制加载器的非常好的隐式方法。

一个尚未在库中(但会有用)的功能是一个部分渴望的评估器,它接受AsyncSeq<'T>并返回一个新的AsyncSeq<'T>,但从源获取一定数量的元素尽快并缓存它们(这样消费者在询问值时就不必等待,只要源可以足够快地产生值)。