functional version for layered cache done