caching XslCompiledTransform
2007-11-13 @ 16:07#
i do a lot of XSLT work in my code. it's my favorite 'big stick.' i'm ready to hit anything with XSLT [grin]. since i also do lots of HTTP/Web work, the current XslCompiledTransform
can be a bit sluggish. on top of that, i use the MvpXslTransform
*excellent* Mvp.Xml Library. a bit more added weight with it's cool EXSLT features, etc.
so i needed a way to optimize the compiled transforms without interfering with my general code pattern. i decided to use the ASP.NET Cache object to hold the compiled transform for later re-use. now, even though i'm coding a state-less web-app, i can still reuse the compiled transform when i want to - sweet!
here's the method i use:
// get the requested xsl file, instance as xsldoc and store in memory for later private MvpXslTransform GetXsl(HttpContext ctx, string xslfile) { return GetXsl(ctx, xslfile, new XmlUrlResolver()); } private MvpXslTransform GetXsl(HttpContext ctx, string xslfile, XmlResolver xmlres) { MvpXslTransform xsldoc = new MvpXslTransform(); xsldoc = (MvpXslTransform)ctx.Cache.Get(xslfile); if (xsldoc == null) { xsldoc = new MvpXslTransform(); xsldoc.Load(xslfile, new XsltSettings(true, false), xmlres); ctx.Cache.Add( xslfile, xsldoc, new System.Web.Caching.CacheDependency(xslfile), System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); } return xsldoc; }