Recent experience-driven behaviour optimizes foraging