【发布时间】:2018-03-13 20:09:33
【问题描述】:
我想使用memoise 包和cache_filesystem 来缓存长时间运行的函数,以便在闪亮的应用程序中使用。这几乎可以完美运行,问题在于输入是一个列表对象,其中包含一个(除其他外)会更改的数据库连接。我想在我的输入对象中忽略这个元素。
数据库连接会随着会话的变化而变化,但我需要memoise 仅查看输入中的id 元素,而不查看列表中的其他元素。有没有办法我可以做到这一点?我查看了... 参数,但这似乎只是进一步限制,而不是放松。
下面的简化示例:
localCache = cache_filesystem("memoiseCache/")
input1_1 = list(id = "id1", dbConn = 100)
input1_2 = list(id = "id1", dbConn = 101)
testFun=function(input) {
print("Running the function")
return(100)
}
library(memoise)
testFun.mem = memoise(testFun)
# This will run the function for the initial time - CORRECT
> testFun.mem(input1_1)
[1] "Running the function"
[1] 100
# This will now fetch the cached result - CORRECT
> testFun.mem(input1_1)
[1] 100
# I need this to ignore the dbConn element and instead fetch the cached result
> testFun.mem(input1_2)
[1] "Running the function"
[1] 100
编辑:我的函数的输入实际上指向一个静态数据库,因此缓存结果没有问题, 指向的静态数据库由id 元素定义,但不同可以连接到同一个数据库。功能可以任意复杂。例如:
function(dbInputObj){
<Many table joins and aggregations>
<Some logic and conditions>
<More table joins>
return(result)
}
【问题讨论】:
标签: r memoization memoise