Implementation of CacheMatrix