Hii team,
I am using DNN API to fetch frequently data by using Ajax. So I did't find answer How to cache API in DNN. Please suggest
public class StockTickerController : DnnApiController {
[HttpGet] public HttpResponseMessage DSIJIncludeTickerGraph() {
return Request.CreateResponse(HttpStatusCode.OK, new { CurrentBseNseValue = AllCurrentBseNseValueVr });
}
I know this is almost a year old however, you can use a CacheHelper class like you would for module data: using DotNetNuke.Common.Utilities; ...
public class CacheHelper { public static bool CacheExists(string key) { return DataCache.GetCache(key) != null; }
public static void SetCache(T toSet, string key) { DataCache.SetCache(key, toSet); }
public static T GetItemFromCache(string key) { return (T)DataCache.GetCache(key); } } Then in the API, you would call the static functions to check if there is cache, or if you need to collect the data from the source. You'll need to supply some sort of key. Here is an example for collecting Events from calendars in Office 365 [AllowAnonymous] [HttpGet] public async Task GetCalendarViews(string UserPrimaries, string start, string end) { var Events = new List(); if (CacheHelper.CacheExists(UserPrimaries)) { Events.AddRange(CacheHelper.GetItemFromCache< List>(UserPrimaries)); } else { // make data calls and get Graph Events ... CacheHelper.SetCache(Events, UserPrimaries); }
return new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(Events.ToJson(), System.Text.Encoding.UTF8, "application/json") }; }
Happy DNNing
Posted By Ryan Jagdfeld on 9/6/2023 5:51 AM I know this is almost a year old however, you can use a CacheHelper class like you would for module data: using DotNetNuke.Common.Utilities; ... public class CacheHelper { public static bool CacheExists(string key) { return DataCache.GetCache(key) != null; } public static void SetCache(T toSet, string key) { DataCache.SetCache(key, toSet); } public static T GetItemFromCache(string key) { return (T)DataCache.GetCache(key); } } Then in the API, you would call the static functions to check if there is cache, or if you need to collect the data from the source. You'll need to supply some sort of key. Here is an example for collecting Events from calendars in Office 365 [AllowAnonymous] [HttpGet] public async Task GetCalendarViews(string UserPrimaries, string start, string end) { var Events = new List(); if (CacheHelper.CacheExists(UserPrimaries)) { Events.AddRange(CacheHelper.GetItemFromCache< List>(UserPrimaries)); } else { // make data calls and get Graph Events ... CacheHelper.SetCache(Events, UserPrimaries); } return new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(Events.ToJson(), System.Text.Encoding.UTF8, "application/json") }; } Happy DNNing
Nice catch on finding and responding to this old question... This is always helpful to our community. Thank you! 💪🏽
Ryan's solution works well but does not protect against a scenario where DNN's cache is cleared and the API endpoint is down. Clearing the cached API data in this scenario would break the application. Is there a way to call the API endpoint before clearing the cached API data and only proceed with clearing that cache if the API call was successful and can be immediately re-cached? I looked into the DNNCacheDependency parameter for the SetCache method, but I'm not sure it accomodates this scenario.
These Forums are dedicated to the discussion of DNN Platform.
For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:
Awesome! Simply post in the forums using the link below and we'll get you started.