Guidance on caching public API data

I am working on a .NET Core API project that has a set of APIs I have developed. Before a user can call the APIs, there is some seed data that needs to be retrieved from a public API. To achieve this, I have a class, SeedData, that grabs the seed data (which is in JSON format) from the public API, deserializes it into an array of objects, and places it in a public list property, Data. I then register this class as a singleton in ConfigureServices, and then inject it into the API controller that I have created.

 public List<Weather> Data {get;set;} public async Task GetSeedData() { var client = new HttpClient { BaseAddress = new System.Uri(BaseUrl) }; var response = await client.GetAsync("weather"); response.EnsureSuccessStatusCode(); var data = await response.Content.ReadAsStringAsync(); Data = JsonConvert.DeserializeObject<List<Weather>>(data); //public property } 

I’d like to get advice on best practices around storing the data in-memory. Is there a better mechanism to store the data rather than on a property? Should the “cache” be on the controller, or within the SeedData class that is injected in? Is there a better way to retrieve & store seed data than what I have described?

Thank you!

submitted by /u/kahtov
[link] [comments]

Leave a Reply