6

MemoryCache如果我希望缓存的数据在多个 WCF 服务中可见(使用PerCall实例模式) ,那么使用该类是一个有效的选项吗?

有两种情况:

  1. 这些服务都托管在 IIS 的同一个应用程序中
  2. 这些服务托管在同一服务器上的不同 IIS 应用程序中
4

1 回答 1

7

1.the services are all hosted in the same app in IIS

如果您使用 MSDN 作为默认缓存对象,答案 是MemoryCache.Default肯定

This property always returns a reference to the default cache instance. For typical application scenarios, only one instance of MemoryCache is required.

你可以像下面这样使用它

ObjectCache cache = MemoryCache.Default;

是否可以通过以下方式进行配置

<system.runtime.caching>
 <memoryCache>
  <namedCaches>
   <add name="Default" physicalMemoryLimitPercentage="20"/>
  </namedCaches>
 </memoryCache>
</system.runtime.caching>

从您的其他服务实例中,您可以访问您的内存缓存,如下所示

List<string> cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();

foreach (string cacheKey in cacheKeys)
          MemoryCache.Default.Remove(cacheKey); 

2.the services are hosted in different IIS applications on the same server

这会有点棘手,但它仍然是一个有效的选项,您可以创建一个专用的 Web 服务进行缓存,其他 Web 服务可以使用netnamedPipeBinding同一服务器上的给定来使用该缓存

于 2014-07-11T14:46:22.093 回答