Suave-控制何时“缓存"或重新计算响应 [英] Suave - Control when responses are 'cached' or recalculated

查看:70
本文介绍了Suave-控制何时“缓存"或重新计算响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何控制何时缓存"响应以及何时重新计算"响应.

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

例如:

[<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

如果我运行上面的网络服务器并击中了几次,则每次获得相同的时间戳.我认为这是有道理的; 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?

在这种情况下,我可能希望为每个请求重新计算" appDemo.我怎么做?我似乎在文档中找不到示例.

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.

推荐答案

尝试一下-不知道它在惯用的和ave"等级上的得分如何:

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)

您是对的,因为您在评估appDemo时捕获了相同的值,所以您看到的是正确的.这是F#的工作方式的特性,与Suave缓存无关.

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.

请注意,WebPart类型是HttpContext -> Async<HttpContext option>函数的别名-因此,从本质上讲,它会针对每个请求重新计算而不是一次计算.

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.

这篇关于Suave-控制何时“缓存"或重新计算响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆