/// <summary> /// Render widget /// Return render result /// </summary> /// <param name="context">Template context</param> /// <param name="widget">Template widget</param> /// <returns></returns> public virtual string RenderWidget(Context context, TemplateWidget widget) { // Get from cache var info = GetWidgetInfo(widget.Path); IKeyValueCache <string, string> renderCache = null; string key = widget.GetCacheKey(); string renderResult = null; if (info.CacheTime > 0) { renderCache = WidgetRenderCache.GetOrAdd(info.CacheBy ?? "", _ => { var cacheFactory = Application.Ioc.Resolve <ICacheFactory>(); var policyNames = info.GetCacheIsolationPolicyNames(); return(cacheFactory.CreateCache <string, string>( CacheFactoryOptions.Default.WithIsolationPolicies(policyNames))); }); renderResult = renderCache.GetOrDefault(key); if (renderResult != null) { return(renderResult); } } // Render widget var renderer = Application.Ioc.Resolve <ITemplateWidgetRenderer>(); renderResult = renderer.Render(context, widget); // Store to cache if (info.CacheTime > 0) { renderCache.Put(key, renderResult, TimeSpan.FromSeconds(info.CacheTime)); } return(renderResult); }
/// <summary> /// Get cached value<br/> /// Generate a new value and store it to cache if the no exist value or exist value expired<br/> /// Attention: This is not an atomic operation<br/> /// 获取缓存值<br/> /// 如果值不存在或已过期则创建一个新的值并储存<br/> /// 注意: 这不是原子操作</summary> /// <param name="cache">Cache</param> /// <param name="key">Key</param> /// <param name="creator">Function to create default value</param> /// <param name="keepTime">Keep time</param> /// <returns></returns> /// <example> /// <code language="cs"> /// var cache = new MemoryCache<int, string>(); /// var cached = cache.GetOrDefault(1, () => "123", TimeSpan.FromSeconds(100)); /// </code> /// </example> public static TValue GetOrCreate <TKey, TValue>( this IKeyValueCache <TKey, TValue> cache, TKey key, Func <TValue> creator, TimeSpan keepTime) { TValue value; if (keepTime == TimeSpan.Zero || !cache.TryGetValue(key, out value)) { value = creator(); cache.Put(key, value, keepTime); } return(value); }
/// <summary> /// Render widget /// Return reder result /// </summary> /// <param name="context">Template context</param> /// <param name="widget">Template widget</param> /// <returns></returns> public virtual string RenderWidget(Context context, TemplateWidget widget) { // Get from cache var info = GetWidgetInfo(widget.Path); IKeyValueCache <string, string> renderCache = null; string key = null; string renderResult = null; if (info.CacheTime > 0) { renderCache = WidgetRenderCache.GetOrAdd(info.CacheBy ?? "", _ => { var cacheFactory = Application.Ioc.Resolve <ICacheFactory>(); var policyNames = info.GetCacheIsolationPolicyNames(); return(cacheFactory.CreateCache <string, string>( CacheFactoryOptions.Default.WithIsolationPolicies(policyNames))); }); key = widget.GetCacheKey(); renderResult = renderCache.GetOrDefault(key); if (renderResult != null) { return(renderResult); } } // Render widget var writer = new StringWriter(); writer.Write($"<div class='template_widget' data-widget='{key}'>"); var scope = Hash.FromAnonymousObject(widget.Args); context.Stack(scope, () => { var includeTag = new Include(); var htmlPath = widget.Path + TemplateWidgetInfo.HtmlExtension; includeTag.Initialize("include", htmlPath, null); includeTag.Render(context, writer); }); writer.Write("</div>"); renderResult = writer.ToString(); // Store to cache if (info.CacheTime > 0) { renderCache.Put(key, renderResult, TimeSpan.FromSeconds(info.CacheTime)); } return(renderResult); }