Use writeReplace before encoding a value for the cache.