8

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.

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

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).

I am using MemoryCache class to store session info in WebAPI service as below.

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

}

My problem is regarding inconsistent behavior of cache.

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.

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

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

I have read about "app pool recycle", 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?

Please some can help me to get out of this issue. Thanks in advance.

Nilesh Wagh
  • 940
  • 1
  • 12
  • 26

3 Answers3

2

You should store client specific information in Session instead of Cache. Cache should be for the whole application (shared)

However, it's not recommended as web api is built with RESTful in mind and RESTful services should be stateless (APIs do not cache state). Stateless applications have many benefits:

  • Reduce Memory Usage
  • Better scalability: Your application scales better. Image what happens if you store information of millions of client at the same time.
  • Better in load balancing scenario: every server can handle every client without losing state.
  • Session expiration problem.

In case you want to store client state, you could do it anyway. Please try the suggestions in the following post: ASP.NET Web API session or something?

In general, caching state locally on the web server is bad (both Session and local MemoryCache). The cache could lose for many reasons:

  • App pool recycle.
  • Load balancing environment
  • Multiple worker processes in IIS

Regarding your requirements:

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).

I'm not sure if you want to update the other clients with new access list immediately when a client sends synchronization call. If that's the case, SignalR would be a better choice.

Otherwise, you could just store the updated access list somewhere (shared cache or even in database) and update the other clients whenever they reconnect with another request.

Community
  • 1
  • 1
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
0

@ScottHanselman said about a bug in .NET 4 here. I hope this fix help you:

The temporary fix:

Create memory cache instance under disabled execution context flow

using (ExecutionContext.SuppressFlow())     {
          // Create memory cache instance under disabled execution context flow
         return new YourCacheThing.GeneralMemoryCache(…);
}

The Hotfix is http://support.microsoft.com/kb/2828843 and you can request it here: https://support.microsoft.com/contactus/emailcontact.aspx?scid=sw;%5BLN%5D;1422

Community
  • 1
  • 1
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
-1

Just a caution, MemoryCache will keep data in memory in single server. So if you have multiple web servers(in front of load balancers), that cache will not be available to other servers. You also use the cache name - "SessionCollection". That data will be shared to all clients. If you need to store data in cache unique to each client, you need to return a token (guid) to the client and use that token to get/update data in cache in subsequent requests.

Try introducing a class level variable. So your code will look like below. (Some code remove for clarity)

private readonly MemoryCache _memCache = MemoryCache.Default;

....

return _memCache.Get("SessionCollection") as List<Session>;

...

_memCache .Add("SessionCollection", value, DateTimeOffset.UtcNow.AddHours(5));
alltej
  • 6,787
  • 10
  • 46
  • 87
  • @alltejThanks for response. I am using single server as well as "Session Collection" data is shared for all clients. Any more suggestion for this kind of data inconsistency? – Nilesh Wagh Sep 16 '16 at 08:50
  • @NileshWagh updated my answer. can you check that out? – alltej Sep 16 '16 at 14:31
  • Hi @alltej, I have added changes according to your edited answer, Bit still I am facing same issue. – Nilesh Wagh Sep 19 '16 at 05:38
  • @NileshWagh can you do a Set on the _memCache instead of Add? This link might help you: http://stackoverflow.com/questions/16018009/memorycache-set-return-removed-cache-item – alltej Sep 23 '16 at 20:11