first version of matrix caching code.