بهینه سازی سرعت یافت ویوها با سفارشی سازی Lookup Caching در Razor View Engine
نویسنده: مهران موسوی
تاریخ: ۱۳۹۳/۰۵/۰۲ ۹:۴۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class CustomViewCache : IViewLocationCache
{
private readonly static string s_key = "_customLookupCach" + Guid.NewGuid().ToString();
private readonly IViewLocationCache _cache;
public CustomViewCache(IViewLocationCache cache)
{
_cache = cache;
}
private static IDictionary<string, string> GetRequestCache(HttpContextBase httpContext)
{
var d = httpContext.Cache[s_key] as IDictionary<string, string>;
if (d == null)
{
d = new Dictionary<string, string>();
httpContext.Cache.Insert(s_key, d, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 15, 0));
}
return d;
}
public string GetViewLocation(HttpContextBase httpContext, string key)
{
var d = GetRequestCache(httpContext);
string location;
if (!d.TryGetValue(key, out location))
{
location = _cache.GetViewLocation(httpContext, key);
d[key] = location;
}
return location;
}
public void InsertViewLocation(HttpContextBase httpContext, string key, string virtualPath)
{
_cache.InsertViewLocation(httpContext, key, virtualPath);
}
} protected void Application_Start() {
ViewEngines.Engines.Clear();
var ve = new RazorViewEngine();
ve.ViewLocationCache = new CustomViewCache(ve.ViewLocationCache);
ViewEngines.Engines.Add(ve);
...
}