Skip to content

Instantly share code, notes, and snippets.

@rickbutterfield
Created March 18, 2022 11:02
Show Gist options
  • Save rickbutterfield/02fa399873745e27d6656416df4db129 to your computer and use it in GitHub Desktop.
Save rickbutterfield/02fa399873745e27d6656416df4db129 to your computer and use it in GitHub Desktop.
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