i have a main Landing page (initial page) in which i need to make a service call. the values of that model will help me determine to show another page/action or not.
lets pick a scenario, Review date if its in past, i will show a new page/action "ReviewData" that is in the same controller .cs class only. If that date is in future, I will show another page/action "Summary" which is also using the same .cs controller class.
Now, if i go to "ReviewData", i need the same service call data that i made above earlier. I don't want to make this service call every now and then, as all these attempts its same value. how do i avoid this and possibly can reuse the data/model values from the first service call?
You need some caching stratergies here is simple cache helper class
using System.Runtime.Caching;
public class cacheservice: ICacheservice
{
public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class
{
T item = MemoryCache.Default.Get(cacheKey) as T;
if (item == null)
{
item = getItemCallback();
MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(10));
}
return item;
}
}
interface ICacheService
{
T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class;
}
Usage:
cacheservice.GetOrSet("CACHEKEY", (delegate method if cache is empty));
Cache provider will check if there's anything by the name of "CACHEKEY" in the cache, and if there's not, it will call a delegate method to fetch data and store it in cache.
Example:
var Data=cacheService.GetOrSet("CACHEKEY", ()=>SomeRepository.GetData())
You can customize according to your needs as well
The answer of the question : - Can reuse the data/model values from the first service call? - Yes. You can achieve this using Tempdata: https://msdn.microsoft.com/en-us/library/system.web.mvc.tempdatadictionary.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.