Implemented cacheMatrix functions

The implementation contains some safeguards around the usage of cached
object.