handle cache writing properly