Added a few unit tests for caching.  Refactored part of the caching code...

... appropriately.