[WIP] Make cache invalidation more generic

First ideas for resolving #4.