我已部署了一个具有协同定位缓存功能的Azure WebRole。 我正在为客户端使用以下默认配置。
<dataCacheClient name="default">
<autoDiscover isEnabled="true" identifier="[name]" />
<!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->
</dataCacheClient>
目前我每次访问缓存时都运行以下代码
DataCacheFactory CacheFactory = new DataCacheFactory();
_Cache = CacheFactory.GetDefaultCache();
这会导致我的应用程序池频繁终止。如何DataCacheFactory
并在需要时重新使用它。
提前致谢
答案 0 :(得分:2)
我建议您使用ASP.NET Application State来保留DataChache Factory对象。
您可以编写一个帮助程序类来获取Data Cache Factory对象。像(从未测试过)的东西:
public class DataCacheHelper
{
public DataCacheHelper()
{
DataCacheFactory factory = new DataCacheFactory();
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["dcf"] = factory;
HttpContext.Current.Application.Unock();
}
public DataCacheFactory GetFactory()
{
var factory = HttpContext.Current.Application["dcf"];
if (factory == null)
{
factory = new DataCacheFactory();
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["dcf"] = factory;
HttpContext.Current.Application.Unock();
}
return factory;
}
}
或者,如果您正在使用ASP.NET MVC - 您可以创建一个基本Controller类,它具有GetCacheFactory方法(正是帮助方法所做的那样),并且让所有控制器继承此基础而不是框架之一。 Web Forms也可以实现同样的目标。