【发布时间】:2019-02-18 09:45:08
【问题描述】:
我m runing a nancyfx with owin on centos 6.5 with mono 5.10.0.140, I change the default ViewLocationProvider to ResourceViewLocationProvider for the default ViewLocationProvider causes memory leak of somekind after running for days, and the ResourceViewLocationProvider dont 也有同样的问题。我想像使用默认 ViewLocationProvider 一样热更新视图,但在谷歌搜索时似乎不可能。
我确实找到了部分解决方案,通过实现自定义 IViewLocator 和 IViewCache,我确实实现了一些热更新。但是除了那些丑陋的静态类之外,感觉并不对劲
//Here is what I did in the custom IViewLocator
//...class definition fallback viewlocator and other staffs
private static ConcurrentDictionary<string, ViewLocationResult> _cachedViewLocationResults;
//..other code
public ViewLocationResult LocateView(string viewName, NancyContext context)
{
//...lock and others
if (_cachedViewLocationResults != null && _cachedViewLocationResults.ContainsKey(viewName))
{
return _cachedViewLocationResults[viewName];
}
//...lock and others
return fallbackViewLocator.LocateView(viewName, context);
}
//...other class
//here is how I update Views
public static void UpdateCachedView(IDictionary<string, ViewLocationResult> replacements)
{
lock (CacheLock)
{
if(_cachedViewLocationResults == null)_cachedViewLocationResults = new ConcurrentDictionary<string, ViewLocationResult>();
foreach (var replace in replacements)
{
_cachedViewLocationResults.AddOrUpdate(replace.Key, x=>replacements[x], (x,y)=>y);
}
}
}
//IViewLocator 结束
//here is what I did in the custom IViewCache
//another static for ViewCache to tell if the view has been updated
public static List<ViewLocationResult> Exceptions { get; private set; }
//...some other code
//here is how I ignore the old cache
public TCompiledView GetOrAdd<TCompiledView>(ViewLocationResult viewLocationResult, Func<ViewLocationResult, TCompiledView> valueFactory)
{
if (Exceptions.Any(x=>x.Name == viewLocationResult.Name && x.Location == viewLocationResult.Location && x.Extension == viewLocationResult.Extension))
{
object old;
this.cache.TryRemove(viewLocationResult, out old);
Exceptions.Remove(viewLocationResult);
}
return (TCompiledView)this.cache.GetOrAdd(viewLocationResult, x => valueFactory(x));
}
有了这些实现和引导程序上的一些设置以及一些 mysql 更新的路由器,我可以按照我想要的方式更新视图,但问题是:
1.现在我必须手动映射 ViewLocationResult 使用的所有位置、名称、扩展名,并且它们太多(243 ...),我想使用某种内置函数来识别更改,类似于 ViewLocationResult 的 IsStale 函数,但我不知道t know which and how...
2. those static class are ugly and I think it could be problematic but I didnt 知道替换它们的更好方法。
谁能给我一个提示,提前谢谢。
【问题讨论】:
标签: c# razor static-methods nancy