How to manage cache in ASP.NET WebApi2?
<p>I have implemented REST service using WebAPI2, service implemeted to manage different sessions which are created and joined by different clients which are accessing service.</p>

<p>Session contains information about access of application functionality and information of participants which have joined same session.</p>

<p>Each client get session information and access list from server for synchronization purpose on every second. According to access changed, client functionality will changed(Enable/Disable).</p>

<p>I am using MemoryCache class to store session info in WebAPI service as below.</p>

<pre><code>public static class SessionManager{
private static object objForLock = new object();
public static List&lt;Session&gt; SessionCollection
{
    get
    {
        lock (objForLock)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get("SessionCollection") as List&lt;Session&gt;;
            // return HttpContext.Current.Application["SessionCollection"] as List&lt;Session&gt;;
        }
    }
    set
    {
        lock (objForLock)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            memoryCache.Add("SessionCollection", value, DateTimeOffset.UtcNow.AddHours(5));
            //HttpContext.Current.Application["SessionCollection"] = value;  
        }
    }
}
</code></pre>

<p>}</p>

<p><strong>My problem is regarding inconsistent behavior of cache.</strong></p>

<p>When clients send synchronization call, it will gives inconsistent results. For some requests, clients gets proper data and for some requests client gets null data alternative after some requests.</p>

<p>I have add debugger and monitor the object for null result, then <em>"memoryCache.Get("SessionCollection")"</em> also null. After some consecutive request it will be proper again. I am not getting why this object is not persistent.</p>

<p>Alternative, I have tried <em>"HttpContext.Current.Application["SessionCollection"]"</em> as well, But same issue is there.</p>

<p>I have read about <strong>"app pool recycle"</strong>, it recycle all cache after particulate time. If my cached object is recycled by app pool recycle, then how can I get this object again?</p>

<p>Please some can help me to get out of this issue. Thanks in advance.</p>