【发布时间】:2011-09-23 22:51:19
【问题描述】:
我在随后的 wcf 数据服务中实现了数据缓存; http://blogs.msdn.com/b/peter_qian/archive/2010/11/17/using-asp-net-output-caching-with-wcf-data-services.aspx
在文章的最后,它建议您可以通过扩展 OutputCacheProvider 来实现自己的自定义缓存提供程序。我已经这样做并测试了缓存工作正常。
但是,我无法让输出缓存与上述文章中所述的数据缓存依赖项一起使用。只要在行中添加;
context.Response.AddCacheItemDependency(cacheDependencyItemKey);
我收到以下错误;
*当使用像“DiskOutputCache”这样的自定义输出缓存提供程序时,仅支持以下过期策略和缓存功能:文件依赖关系、绝对过期、静态验证回调和静态替换回调。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Configuration.Provider.ProviderException:当使用像“DiskOutputCache”这样的自定义输出缓存提供程序时,仅支持以下过期策略和缓存功能:文件依赖关系、绝对过期、静态验证回调和静态替换回调.
来源错误:
在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
堆栈跟踪:
[ProviderException:当使用像“DiskOutputCache”这样的自定义输出缓存提供程序时,仅支持以下过期策略和缓存功能:文件依赖、绝对过期、静态验证回调和静态替换回调。] System.Web.Caching.OutputCache.InsertResponse(String cachedVaryKey, CachedVary cachedVary, String rawResponseKey, CachedRawResponse rawResponse, CacheDependency 依赖, DateTime absExp, TimeSpan slipExp) +2905959 System.Web.Caching.OutputCacheModule.OnLeave(对象源,EventArgs eventArgs)+107 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75***
protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
base.OnStartProcessingRequest(args);
HttpContext context = HttpContext.Current;
if (context.Cache.Get(cacheDependencyItemKey) == null) {
// what the cache item value is doesn't really matter
context.Cache.Insert(cacheDependencyItemKey, "Item");
}
if (context.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase) ||
context.Request.HttpMethod.Equals("MERGE", StringComparison.OrdinalIgnoreCase) ||
context.Request.HttpMethod.Equals("PUT", StringComparison.OrdinalIgnoreCase) ||
context.Request.HttpMethod.Equals("DELETE", StringComparison.OrdinalIgnoreCase)) {
context.Cache.Remove(cacheDependencyItemKey);
} else {
// set cache policy to this page
HttpCachePolicy cachePolicy = context.Response.Cache;
// server&private: server and client side cache only
cachePolicy.SetCacheability(HttpCacheability.ServerAndPrivate);
// default cache expire: never
cachePolicy.SetExpires(DateTime.MaxValue);
// cached output depends on: accept, charset, encoding, and all parameters (like $filter, etc)
cachePolicy.VaryByHeaders["Accept"] = true;
cachePolicy.VaryByHeaders["Accept-Charset"] = true;
cachePolicy.VaryByHeaders["Accept-Encoding"] = true;
cachePolicy.VaryByParams["*"] = true;
cachePolicy.SetValidUntilExpires(true);
// add data cache dependency
context.Response.AddCacheItemDependency(cacheDependencyItemKey);
}
}
此错误似乎表明您不能像这样在 WCF 中使用 CacheItemDependency。我很困惑,因为它似乎在 msdn 博客文章中有效。
有什么想法吗?
批量更新似乎有问题
在数据服务中,当您使用 DataServiceContext 并执行 SaveChanges(SaveChangesOptions.Batch); 请求 uri 看起来像“~/$batch”所以没有这似乎是一种简单的方法来实际确定正在创建、更新或删除的内容以试图使这些项目无效。
我假设在下面的逻辑块中,对于 Web 服务数据的更改,您必须直接在缓存上调用 remove,并让底层缓存提供程序计算出需要删除的内容。但似乎没有一种简单的方法来实际确定发生了什么变化。我在这里遗漏了什么吗?
【问题讨论】:
标签: asp.net wcf caching .net-4.0