Refactor header reading/writing in cache