R memoise to filesystem with a non-identical match condition

194 Views Asked by At

I would like to use the memoise package with cache_filesystem to cache long running functions for use in a shiny application. This almost works perfectly, the problem is that the input is a list object that contains a (amongst other things) a db connection which will change. I want to ignore this element in my input object.

The db connection will change from session to session, but I need memoise to only look at the id element in the input, and not the other elements in the list. Is there a way I can do this? I have looked at the ... argument but this seems to only further restrict, not relax.

Simplified example below:

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

EDIT: The input to my function actually points to a static DB so there are no issues with caching the results, which static DB is pointed to is defined by the id element, but different connections can be made to the same DB. The functions can be arbitrarily complicated. For example:

function(dbInputObj){
  <Many table joins and aggregations>
  <Some logic and conditions>
  <More table joins>
  return(result)
}
1

There are 1 best solutions below

2
On

You can write a wrapper function to hide the differences. I cannot see how your app works from the simplified code though.

Are you connecting to same database with different dbConn? Are you trying to cache the database inquiry or some calculation after database inquiry?

I don't think it's a good idea to cache database inquiry if the dbConn can be different. You are supposed to get updated data from database, not caching it.

If you want to cache the calculation after database inquiry, just get the value first, then memoise the calculation function with the values only, not the database connection.