modified CACHEMATRIX.R