Created
March 18, 2022 11:02
-
-
Save rickbutterfield/02fa399873745e27d6656416df4db129 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MovieSerivce : IMovieService | |
{ | |
private readonly IAppPolicyCache _runtimeCache; | |
private const string MOVIES_CACHE_KEY = "Movies.API.Cache"; | |
public MovieSerivce(AppCaches appCaches) | |
{ | |
_runtimeCache = appCaches.RuntimeCache; | |
} | |
public IEnumerable<Movie> GetAllMovies() | |
{ | |
IEnumerable<Movie> movies = _runtimeCache.GetCacheItem(MOVIES_CACHE_KEY, () => | |
{ | |
IEnumerable<Movie> movieList = default; | |
using (HttpClient httpClient = new HttpClient()) | |
{ | |
httpClient.BaseAddress = new Uri(MOVIE_BASE_URL); | |
HttpResponseMessage response = httpClient.GetAsync(API_ENDPOINT).Result; | |
if (response.IsSuccessStatusCode) | |
{ | |
var result = response.Content.ReadAsStringAsync().Result; | |
movieList = JsonConvert.DeserializeObject<IEnumerable<Movie>>(result); | |
} | |
} | |
return movieList; | |
}, TimeSpan.FromMinutes(30), false); | |
return movies; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment