I need to frequently access the result of a time-consuming calculation. The result changes infrequently, so I have to recalculate the data from time to time but it is ok to use the outdated result for a while. What would be the easiest way to do this and is there an existing library method or design pattern?
I am thinking of something like
private static List myCachedList = null;
...
// refresh list once in 3600 seconds
if (needsRefresh(myCachedList, 3600)) {
// run the calculation
myCachedList = ...
}
// use either updated or previous value from here on
A proper implementation might not be trivial, it might have to deal with thread safety, race conditions etc., so I would rather use a proven implementation than roll my own here.