Adding code for matrix with inbuilt cache.