Suave - control when responses are "cached" or recounted

I want to understand how to control when responses are “cached”, when they are “recounted”.

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 aforementioned web server and hit it several times, then every time I get the same timestamp. I think that makes sense; appDemo is just an expression that evaluates the first time and never again, right?

In this case, I would like appDemo be "recounted" for each request. How can I do it? I can not find an example in the docs.

+5
source share
1 answer

Try this - I'm not sure how much it is rated on the "idiomatic Suave scale":

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

You are right in that you see the same value, because it was fixed at the moment appDemo is evaluated. This is a feature of how F # works, and has nothing to do with caching it.

Please note that the WebPart type is an alias for the HttpContext -> Async<HttpContext option> function - therefore, by its very nature, it allows itself to be recalculated for each request, and not calculated once.

+9
source

Source: https://habr.com/ru/post/1257893/


All Articles