/// <summary> /// Initialize /// </summary> public PathManager() { var configManager = Application.Ioc.Resolve <ConfigManager>(); TemplatePathCacheTime = TimeSpan.FromSeconds( configManager.WebsiteConfig.Extra.GetOrDefault(ExtraConfigKeys.TemplatePathCacheTime, 2)); ResourcePathCacheTime = TimeSpan.FromSeconds( configManager.WebsiteConfig.Extra.GetOrDefault(ExtraConfigKeys.ResourcePathCacheTime, 2)); TemplatePathCache = new IsolatedMemoryCache <string, string>("Device"); ResourcePathCache = new IsolatedMemoryCache <string, string>("Device"); }
/// <summary> /// 初始化 /// </summary> public CartProductManager() { var configManager = Application.Ioc.Resolve <ConfigManager>(); CartProductTotalCountCacheTime = TimeSpan.FromSeconds( configManager.WebsiteConfig.Extra.GetOrDefault(ExtraConfigKeys.CartProductTotalCountCacheTime, 3)); CartProductTotalCountCache = new IsolatedMemoryCache <CartProductType, long?>("Ident"); SessionExpireDaysForNonUserPurcharse = TimeSpan.FromDays( configManager.WebsiteConfig.Extra.GetOrDefault( ExtraConfigKeys.SessionExpireDaysForNonUserPurcharse, 1)); }
/// <summary> /// 初始化 /// </summary> public ArticleManager() { var configManager = Application.Ioc.Resolve <ConfigManager>(); ArticleApiInfoCacheTime = TimeSpan.FromSeconds( configManager.WebsiteConfig.Extra.GetOrDefault(ExtraConfigKeys.ArticleApiInfoCacheTime, 15)); ArticleApiInfoCache = new IsolatedMemoryCache <long, object>("Ident", "Locale"); ArticleSearchResultCacheTime = TimeSpan.FromSeconds( configManager.WebsiteConfig.Extra.GetOrDefault(ExtraConfigKeys.ArticleSearchResultCacheTime, 15)); ArticleSearchResultCache = ( new IsolatedMemoryCache <int, StaticTableSearchResponse>("Ident", "Locale", "Url")); }
/// <summary> /// Initialize /// </summary> public TemplateAreaManager() { var configManager = Application.Ioc.Resolve <ConfigManager>(); WidgetInfoCacheTime = TimeSpan.FromSeconds( configManager.WebsiteConfig.Extra.GetOrDefault(ExtraConfigKeys.WidgetInfoCacheTime, 2)); CustomWidgetsCacheTime = TimeSpan.FromSeconds( configManager.WebsiteConfig.Extra.GetOrDefault(ExtraConfigKeys.CustomWidgetsCacheTime, 2)); Areas = new Dictionary <string, TemplateArea>(); WidgetInfoCache = new IsolatedMemoryCache <string, TemplateWidgetInfo>("Device"); CustomWidgetsCache = new IsolatedMemoryCache <string, List <TemplateWidget> >("Device"); WidgetRenderCache = new ConcurrentDictionary <string, IsolatedMemoryCache <string, string> >(); }
/// <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); IsolatedMemoryCache <string, string> renderCache = null; string key = null; string renderResult = null; if (info.CacheTime > 0) { renderCache = WidgetRenderCache.GetOrAdd(info.CacheBy ?? "", _ => new IsolatedMemoryCache <string, string>(info.GetCacheIsolationPolicyNames())); 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); }
public void All() { var cache = new IsolatedMemoryCache <string, string>("Locale"); // Set LocaleUtils.SetThreadLanguage("zh-CN"); cache.Put("Key", "ValueForCN", TimeSpan.FromSeconds(5)); LocaleUtils.SetThreadLanguage("en-US"); cache.Put("Key", "ValueForUS", TimeSpan.FromSeconds(5)); // Get LocaleUtils.SetThreadLanguage("zh-CN"); Assert.Equals(cache.GetOrDefault("Key"), "ValueForCN"); LocaleUtils.SetThreadLanguage("en-US"); Assert.Equals(cache.GetOrDefault("Key"), "ValueForUS"); // Remove LocaleUtils.SetThreadLanguage("zh-CN"); cache.Remove("Key"); Assert.Equals(cache.GetOrDefault("Key"), null); LocaleUtils.SetThreadLanguage("en-US"); Assert.Equals(cache.GetOrDefault("Key"), "ValueForUS"); cache.Remove("Key"); Assert.Equals(cache.GetOrDefault("Key"), null); }