/// <summary> /// Returns LavaTemplate object from cache. If template does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="engine">The key.</param> /// <param name="key">The key.</param> /// <param name="content">The content.</param> /// <returns></returns> public WebsiteLavaTemplateCache Get(ILavaEngine engine, string key, string content) { WebsiteLavaTemplateCache template; // If cache items need to be serialized, do not cache the template because it isn't serializable. if (RockCache.IsCacheSerialized) { template = Load(engine, content); } else { var fromCache = true; template = WebsiteLavaTemplateCache.GetOrAddExisting(key, () => { fromCache = false; return(Load(engine, content)); }); if (fromCache) { Interlocked.Increment(ref _cacheHits); } else { Interlocked.Increment(ref _cacheMisses); } } return(template); }
void ILavaTemplateCacheService.ClearCache() { WebsiteLavaTemplateCache.Clear(); _cacheHits = 0; _cacheMisses = 0; }
void ILavaTemplateCacheService.AddTemplate(ILavaTemplate template, string cacheKey) { if (string.IsNullOrWhiteSpace(cacheKey)) { throw new Exception("WebsiteLavaTemplateCache template add failed. A cache key must be specified."); } WebsiteLavaTemplateCache.UpdateCacheItem(cacheKey, new WebsiteLavaTemplateCache() { Template = template }); }
private WebsiteLavaTemplateCache Load(ILavaEngine engine, string content) { if (engine == null) { throw new Exception("WebsiteLavaTemplateCache template load failed. The cache must be initialized for a specific engine."); } var result = engine.ParseTemplate(content); var cacheEntry = new WebsiteLavaTemplateCache { Template = result.Template }; return(cacheEntry); }
/// <summary> /// Gets a flag indicating if the cache contains the specified template. /// </summary> /// <param name="key"></param> /// <returns></returns> public bool Contains(string key) { if (RockCache.IsCacheSerialized) { return(false); } bool contains = true; var template = WebsiteLavaTemplateCache.GetOrAddExisting(key, () => { contains = false; return(null); }); return(contains); }
void ILavaTemplateCacheService.RemoveKey(string key) { WebsiteLavaTemplateCache.Remove(key); }
void ILavaTemplateCacheService.RemoveTemplate(string content) { var key = GetTemplateKey(content); WebsiteLavaTemplateCache.Remove(key); }