Refactoring cache implementation