caching - 无法将 LazyCache 与 Suave 的 WebPart 一起使用
问题描述
我正在尝试使用 LazyCache ( https://github.com/alastairtree/LazyCache ) 来缓存一些 API 请求。
代码如下:
let private cache = CachingService()
let doAPIStuff some parameters : WebPart = ...
let result = cache.GetOrAdd(hashedRequest, (fun _ -> doAPIStuff))
但我得到这个编译错误:
WebAPI.fs(59, 17): [FS0041] No overloads match for method 'GetOrAdd'.
Known types of arguments: string * ('a -> WebPart)
Available overloads:
- (extension) IAppCache.GetOrAdd<'T>(key: string, addItemFactory: Func<'T>) : 'T // Argument 'addItemFactory' doesn't match
- CachingService.GetOrAdd<'T>(key: string, addItemFactory: Func<Extensions.Caching.Memory.ICacheEntry,'T>) : 'T // Argument 'addItemFactory' doesn't match
这些是可用的类型:
所以我可以这样做:
let doAPIStuff some parameters : Object = ...
并将我的 WebPart 装箱,它工作正常。我知道 WebPart 是一个函数(在另一个问题中感谢 Fyodor),但我不明白为什么函数本身不能作为对象在缓存中。
解决方案
我认为在这种情况下您需要显式创建Func
委托,否则 F# 编译器无法区分这两个重载。
第二个参数的类型(在基本情况下)是Func<'T>
一个函数,获取unit
并返回要缓存的值。这也意味着,在这个函数内部,您应该doAPIStuff
使用参数作为参数进行调用。
假设这是在一些接受 , 的actualRequest
处理程序中some
,parameters
以下应该可以工作:
let cache = CachingService()
let doAPIStuff some parameters : WebPart =
failwith "!"
let actualRequest some parameters =
let hashedRequest = some + parameters
let result =
cache.GetOrAdd(hashedRequest,
Func<_>(fun () -> doAPIStuff some parameters))
result
推荐阅读
- javascript - 使用 cURL 与 php 进行 AJAX 调用 - 找不到 405 方法
- sql-server - 带有 nvarchar 的 T-SQL Where 子句由 openquery 中的 string_agg 组成
- vue.js - 如何将 VueJS 路由器链接 ID 增加 1?
- python - 构建具有多个图的网络
- java - 如何使用 JDBC 连接到 Oracle 数据库
- reactjs - Google 功能更正 IAM 设置以防止嵌套资源出现 403 错误
- java - 将调用外部 API 的业务逻辑放入 Spring Batch 编写器是否是个好主意?(将信息持久化到另一个系统中)
- github - Pycharm 或终端完成的推送不计入 github 的提交日历
- r - R中network包中类似于igraph的v()的函数
- angular - angular-oauth2-oidc:redirect_uri 必须是 /index.html 吗?