Cache: small refactorization of key-encoding; tests added