So, I have this small block of code that will perform several tasks in parallel.
var activityList = await dataService.GetActivitiesAsync();
var results = (from activity in activityList
select new {
Activity = activity,
AthleteTask = dataService.GetAthleteAsync(activity.AthleteID)
}).ToList();
await Task.WhenAll(results.Select(t => t.AthleteTask));
foreach(var pair in results)
{
pair.Activity.Athlete = pair.AthleteTask.Result;
}
So, I load the athlete data for each given action. But maybe we ask one athlete several times. How can we guarantee that the GetAthleteAsync method will only work online to receive actual data if it is not already in our memory cache?
I have currently tried using ConcurrentDictionary<int, Athelete>GetAthleteAsync inside the method
private async Task<Athlete> GetAthleteAsync(int athleteID)
{
if(cacheAthletes.Contains(athleteID))
return cacheAthletes[atheleID];
** else fetch from web
}
source
share