cachematrix implementation