Suave - Control when responses are 'cached' or recalculated

夙愿已清 提交于 2019-12-12 10:56:28

问题


I want to understand how to control when responses are 'cached' versus when they are 'recalculated'.

As an example:

[<EntryPoint>]
let main [| port |] =

    let config =
        { defaultConfig with
                bindings = [ HttpBinding.mk HTTP IPAddress.Loopback (uint16 port) ]
                listenTimeout = TimeSpan.FromMilliseconds 3000.
                }

    let appDemo:WebPart = 
        DateTime.Now.ToString()
        |> sprintf "Server timestamp: %s"
        |> Successful.OK

    startWebServer config appDemo

If I run the above webserver and hit it several times then each time I get the same timestamp back. Which I guess makes sense; appDemo is just an expression which is calculated first time around and never again, right?

In this circumstance, I might want appDemo to be 'recalculated' for every request. How do I do that? I can't seem to find an example in the docs.


回答1:


Try this - not sure how high it scores on "idiomatic Suave" scale though:

let appDemo:WebPart = 
    request (fun req -> 
        DateTime.Now.ToString()
        |> sprintf "Server timestamp: %s"
        |> Successful.OK)

You're right in that you're seeing the same value because it's captured at the time appDemo is evaluated. That's a property of how F# works however, and has nothing to do with Suave caching it.

Note that WebPart type is an alias for HttpContext -> Async<HttpContext option> function - so inherently it yields itself to being recalculated on each request rather than being calculated once.



来源:https://stackoverflow.com/questions/39931078/suave-control-when-responses-are-cached-or-recalculated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!