Write an R function is able to cache potentially time-consuming computations