Detect cache changes properly.

Also add explicit tests for caching.