【发布时间】:2014-09-02 04:00:25
【问题描述】:
如果我希望缓存数据在多个 WCF 服务中可见(使用 PerCall 实例模式),使用 MemoryCache 类是否是一个有效选项?
有两种情况:
- 服务都托管在 IIS 的同一个应用中
- 服务托管在同一服务器上的不同 IIS 应用程序中
【问题讨论】:
标签: c# wcf caching iis memorycache
如果我希望缓存数据在多个 WCF 服务中可见(使用 PerCall 实例模式),使用 MemoryCache 类是否是一个有效选项?
有两种情况:
【问题讨论】:
标签: c# wcf caching iis memorycache
1.the services are all hosted in the same app in IIS
如果您使用MemoryCache.Default 作为默认缓存对象,答案是肯定的
来自 MSDN
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 给定位于同一服务器上的其他 web 服务
【讨论】: