/// <summary> /// 分析 HTML 文档,此方法会根据情况缓存文档模型 /// </summary> /// <param name="contentResult">文档加载结果</param> /// <param name="parser">HTML 解析器</param> /// <returns>HTML 文档对象</returns> public static IHtmlDocument ParseDocument(HtmlContentResult contentResult, IHtmlParser parser) { var domProvider = parser.DomProvider; if (contentResult.CacheKey != null && domProvider != null)//如果可以缓存 { var key = contentResult.CacheKey; var cacheKey = string.Format(CultureInfo.InvariantCulture, DocumentCacheKey, contentResult.VirtualPath); var createDocument = Cache.Get(cacheKey) as Func <IHtmlDomProvider, IHtmlDocument>; if (createDocument != null) { return(createDocument(domProvider)); } WebServiceLocator.GetTraceService().Trace(TraceLevel.Info, "Jumony Web", "Document cache missed"); var document = ParseDocument(parser, contentResult.Content, contentResult.VirtualPath); createDocument = document.Compile();//必须同步编译文档,否则文档对象可能被修改。 new Action(delegate { createDocument(domProvider);//可以异步预热,预热后再存入缓存。 Cache.Insert(cacheKey, createDocument, new CacheDependency(new string[0], new[] { key }), CacheItemPriority.High); } ).BeginInvoke(null, null);//立即在新线程预热此方法 return(document); } else { return(ParseDocument(parser, contentResult.Content, contentResult.VirtualPath)); } }
/// <summary> /// 写入追踪信息 /// </summary> /// <param name="message">消息内容</param> /// <param name="category">消息分类</param> /// <param name="level">消息级别</param> protected void Trace(string message, string category = null, TraceLevel level = TraceLevel.Info) { WebServiceLocator.GetTraceService().Trace(level, category ?? "Jumony Web", message); }
/// <summary> /// 写入一条追踪信息 /// </summary> /// <param name="message">追踪消息</param> protected virtual void Trace(string message) { WebServiceLocator.GetTraceService().Trace(TraceLevel.Info, "Jumony Partial", message); }