Fix memory leak in cache_save_map, refactor, work on mapfile IO.