My project using ASP.NET MVC5(.net 4.5.1) on Windows Server 2012 R2 IIS8.5
I have a class for get and set some cache
public class TheCache
{
#region Instance
private static TheCache caches;
private TheCache() { }
public static TheCache Instance
{
get
{
if (null == caches)
caches = new TheCache();
return caches;
}
}
#endregion
// Set the cache
public void SetCache(key, data, time){
MemoryCache.Default.Set(key, data, time);
}
// Get the cache
public object GetCache(key){
return MemoryCache.Default.Get(key);
}
}
Now the problem is, I have a Action in a Controller
public ActionResult SomeActionInController(){
// this place always execute in every request
if (null == TheCache.Instance.GetCache("key")
TheCache.Instance.SetCache("key", "value", Datetime.Now.AddHours(1));
}
But I will get null in every requests. So Why? Is there any way to fixed it? Thank you.
Answer: Static property always null in every request after set value on ASP.NET MVC5