简体   繁体   English

在WCF服务中实现缓存

[英]Implementing a cache in a WCF Service

I have a WCF service that caches certain data and uses it to respond to web requests. 我有一个WCF服务,它缓存某些数据并使用它来响应Web请求。 To deal with this requirement, I made the service a Singleton (using InstanceContextMode.Single and ConcurrencyMode.Multiple (yes, it's threadsafe)). 为了处理这个需求,我使服务成为Singleton(使用InstanceContextMode.SingleConcurrencyMode.Multiple (是的,它是线程安全的))。

I've tried to set the timeout of the service to its maximum using the following binding: 我尝试使用以下绑定将服务的超时设置为其最大值:

 <binding name="WebHttpBinding" receiveTimeout="24.20:31:23.6470000">
    <security mode="TransportCredentialOnly">
      <transport clientCredentialType="None" />
    </security>
 </binding>

My problem is that the service instance dies at unpredictable intervals, meaning the first web request to hit will cause the cache to get rebuilt (a very slow process). 我的问题是服务实例以不可预测的间隔死亡,这意味着第一个要命中的Web请求将导致缓存重建(一个非常缓慢的过程)。

Ideally, the cache would rebuild at a set time each day without having to get hit by a web request. 理想情况下,缓存将在每天的设定时间重建,而不必受到Web请求的影响。 I could set the app pool to recycle at a set time, but this still wouldn't resolve the issue of the service not getting instantiated until the first web request. 我可以将应用程序池设置为在设定的时间回收,但是这仍然无法解决在第一次Web请求之前未实例化的服务问题。 I'd rather not have to make a little scheduled script that sends a request to the service, as that is kind of hacky. 我宁愿不必制作一个向服务发送请求的预定脚本,因为这有点像hacky。

Is there a better strategy for performing caching in a WCF service? 是否有更好的策略在WCF服务中执行缓存? What have others done here? 其他人在这做了什么? Is there a best practice? 有最好的做法吗?

There is an MSDN article on Caching Support for WCF Web HTTP Services , an excerpt is quoted below: 关于WCF Web HTTP服务的缓存支持的MSDN文章,摘录如下:

The .NET Framework version 4 enables you to use the declarative caching mechanism already available in ASP.NET in your WCF Web HTTP services. .NET Framework版本4使您可以在WCF Web HTTP服务中使用ASP.NET中已有的声明性缓存机制。 This allows you to cache responses from your WCF Web HTTP service operations. 这允许您缓存来自WCF Web HTTP服务操作的响应。 When a user sends an HTTP GET to your service that is configured for caching, ASP.NET sends back the cached response and the service method is not called. 当用户向配置为缓存的服务发送HTTP GET时,ASP.NET会发回缓存的响应,并且不会调用服务方法。 When the cache expires, the next time a user sends an HTTP GET, your service method is called and the response is once again cached.......... 当缓存过期时,下次用户发送HTTP GET时,将调用您的服务方法并再次缓存响应..........

You might also want to look at: 您可能还想看一下:

I have implemented caching at a higher layer inside the webservice. 我已经在webservice中的更高层实现了缓存。

This way you can decide when to invalidate the cache, and when to deserialize from disk. 这样,您可以决定何时使缓存无效,以及何时从磁盘反序列化。

To make sure the cache is built before the first webrequest, add some code to global.asax to generate the cache upon load of the web server. 要确保在第一次webrequest之前构建缓存,请在global.asax添加一些代码,以便在加载Web服务器时生成缓存。

This is much simpler than doing it the " right way " 这比“ 正确的方式简单得多

[OperationContract]
public void GetLargeComplexData();

public GetLargeComplexData()
{
   // deserialize last cached data from db or file
   ...

   // Verify the deserialized cache is not invalid
   ...

   // if cache is invalid rebuild
   ...

   //return cached data
   ...
}

The receiveTimeout isn't going to affect what you're trying to do. receiveTimeout不会影响您尝试执行的操作。 You should use AppFabric to keep your service always running. 您应该使用AppFabric来保持您的服务始终运行。 That way, whenever you recycle, AppFabric will automatically warm up your service. 这样,无论何时回收,AppFabric都会自动为您的服务热身。 Just make sure that your cache gets built when your service is instantiated, not when it's first accessed. 只需确保在实例化服务时构建缓存,而不是在首次访问时。

An option you can use is to move the cache out of the WCF service and into a dedicated cache service such as Memcached or use Microsoft AppFabric Caching 您可以使用的一个选项是将缓存移出WCF服务并转移到专用缓存服务(如Memcached)或使用Microsoft AppFabric缓存

This allows you to separate the storage of cached data from the WCF service so you have more freedom in your architecture of how the data is managed and accessed. 这允许您将缓存数据的存储与WCF服务分开,这样您就可以更自由地在架构中管理和访问数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM