public async Task AddOrUpdate(string key, TimeSpan expiration, object value) { await _memoryCacheService.AddOrUpdate(key, expiration, value); if (expiration == TimeSpan.MaxValue) { await _permanentStorageService.AddOrUpdate(key, expiration, value); } }
private async Task <IReadOnlyList <SummaryActivity> > _GetActivities(string accessToken, long athleteId) { var activitiesCache = await _permanentStorageService.Get <ActivitiesCache>($"ActivitiesCache:{athleteId}"); if (activitiesCache == null) { activitiesCache = new ActivitiesCache { Activities = new List <SummaryActivity>(), LastSyncDate = DateTimeOffset.UtcNow.AddYears(-10) }; } var result = new List <SummaryActivity>(); IReadOnlyList <SummaryActivity> activities; var page = 1; do { activities = await Get <IReadOnlyList <SummaryActivity> >( accessToken, $"https://www.strava.com/api/v3/athlete/activities?page={page}&per_page=200&after={activitiesCache.LastSyncDate.ToUnixTimeSeconds()}"); result.AddRange(activities); page++; } while (activities.Any()); activitiesCache.Activities.RemoveAll(activity => result.Any(a => a.Id == activity.Id)); activitiesCache.Activities.AddRange(result); activitiesCache.Activities = activitiesCache.Activities.OrderByDescending(activity => activity.StartDate).ToList(); activitiesCache.LastSyncDate = DateTimeOffset.UtcNow.AddMonths(-1); await _permanentStorageService.AddOrUpdate($"ActivitiesCache:{athleteId}", TimeSpan.MaxValue, activitiesCache); return(activitiesCache.Activities.Where(IsValidActivityType).ToList()); }