cache refactor and tests