如何在ASP.NET WebApi2管理缓存? [英] How to manage cache in ASP.NET WebApi2?

查看:408
本文介绍了如何在ASP.NET WebApi2管理缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用WebAPI2实现REST服务,服务Implemeted一个管理其中创建和由被访问的服务不同的客户端加入了不同的会话。

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

我使用MemoryCache类存储在下面的WebAPI服务会话信息。

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;  
        }
    }
}



}

}

我的问题是关于高速缓存的不一致的行为。

当客户端发送同步调用,它将给不一致的结果。对于一些要求,客户得到正确的数据和一些请求客户端获得一些要求后,空数据替代。

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.

我有加调试器和监视空结果的对象,那么memoryCache.Get(SessionCollection)的也为空。一些连续的请求后,它会再次正确。我没有得到为什么这个对象不是永久性的。

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.

另类,我都试过的HttpContext.Current.Application [SessionCollection]的为好,但同样的问题是存在的。

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.

推荐答案

您应该存储客户的具体信息在会话而不是缓存缓存应该是为整个应用程序(共享)

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

然而,这不是推荐的Web API构建REST风格记和RESTful服务应该是无状态(的API 不要缓存状态)。无状态的应用程序有很多好处:

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.

如果你要存储客户端的状态,你可以做也无妨。请尝试以下职位的建议:的ASP.NET Web API会话还是什么?

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?

在一般情况下,高速缓存状态在本地上的Web服务器是坏的(包括会话和地方的MemoryCache )。缓存可能丢失的原因有很多:

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


  • 应用程序池回收

  • 负载均衡环境

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

我不知道,如果你想更新其他用户提供了新的访问列表立即客户端发送同步调用。如果是这样的情况下, SignalR 会是一个更好的选择。

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.

这篇关于如何在ASP.NET WebApi2管理缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆