Skip to content
This repository has been archived by the owner on Sep 14, 2018. It is now read-only.

Commit

Permalink
Athlete and Club in memory Task cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Depechie committed Mar 1, 2016
1 parent 9dbe225 commit 5ff3181
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
12 changes: 11 additions & 1 deletion src/Kliva/Services/StravaAthleteService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Kliva.Models;
using Kliva.Services.Interfaces;
Expand All @@ -9,6 +10,10 @@ namespace Kliva.Services
public class StravaAthleteService : IStravaAthleteService
{
private readonly ISettingsService _settingsService;

//TODO: Glenn - When to Invalidate cache?
private readonly ConcurrentDictionary<string, Task<AthleteSummary>> _cachedAthleteTasks = new ConcurrentDictionary<string, Task<AthleteSummary>>();

public Athlete Athlete { get; set; }

public StravaAthleteService(ISettingsService settingsService)
Expand Down Expand Up @@ -47,7 +52,12 @@ public async Task<Athlete> GetAthleteAsync()
/// </summary>
/// <param name="athleteId">The Strava Id of the athlete.</param>
/// <returns>The AthleteSummary object of the athlete.</returns>
public async Task<AthleteSummary> GetAthleteAsync(string athleteId)
public Task<AthleteSummary> GetAthleteAsync(string athleteId)
{
return _cachedAthleteTasks.GetOrAdd(athleteId, GetAthleteFromServiceAsync);
}

private async Task<AthleteSummary> GetAthleteFromServiceAsync(string athleteId)
{
try
{
Expand Down
31 changes: 20 additions & 11 deletions src/Kliva/Services/StravaClubService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using Kliva.Helpers;
Expand All @@ -11,6 +12,9 @@ public class StravaClubService : IStravaClubService
{
private readonly ISettingsService _settingsService;

//TODO: Glenn - When to Invalidate cache?
private readonly ConcurrentDictionary<string, Task<Club>> _cachedClubTasks = new ConcurrentDictionary<string, Task<Club>>();

public StravaClubService(ISettingsService settingsService)
{
_settingsService = settingsService;
Expand Down Expand Up @@ -43,15 +47,25 @@ public async Task<List<ClubSummary>> GetClubsAsync()
/// </summary>
/// <param name="clubId">The id of the club.</param>
/// <returns>The Club object containing detailed information about the club.</returns>
public async Task<Club> GetClubAsync(string clubId)
public Task<Club> GetClubAsync(string clubId)
{
return _cachedClubTasks.GetOrAdd(clubId, GetClubFromServiceAsync);
}

/// <summary>
/// Gets the members of the specified club.
/// </summary>
/// <param name="clubId">The club's id.</param>
/// <returns>The club's members.</returns>
public async Task<List<AthleteSummary>> GetClubMembersAsync(string clubId)
{
try
{
var accessToken = await _settingsService.GetStoredStravaAccessToken();
string getUrl = $"{Endpoints.Club}/{clubId}?access_token={accessToken}";
string getUrl = $"{Endpoints.Club}/{clubId}/members?access_token={accessToken}";
string json = await WebRequest.SendGetAsync(new Uri(getUrl));

return Unmarshaller<Club>.Unmarshal(json);
return Unmarshaller<List<AthleteSummary>>.Unmarshal(json);
}
catch (Exception ex)
{
Expand All @@ -61,20 +75,15 @@ public async Task<Club> GetClubAsync(string clubId)
return null;
}

/// <summary>
/// Gets the members of the specified club.
/// </summary>
/// <param name="clubId">The club's id.</param>
/// <returns>The club's members.</returns>
public async Task<List<AthleteSummary>> GetClubMembersAsync(string clubId)
public async Task<Club> GetClubFromServiceAsync(string clubId)
{
try
{
var accessToken = await _settingsService.GetStoredStravaAccessToken();
string getUrl = $"{Endpoints.Club}/{clubId}/members?access_token={accessToken}";
string getUrl = $"{Endpoints.Club}/{clubId}?access_token={accessToken}";
string json = await WebRequest.SendGetAsync(new Uri(getUrl));

return Unmarshaller<List<AthleteSummary>>.Unmarshal(json);
return Unmarshaller<Club>.Unmarshal(json);
}
catch (Exception ex)
{
Expand Down

0 comments on commit 5ff3181

Please sign in to comment.