Make cache invalidation more generic

Resolve #4.