Beispiel #1
0
        /// <summary>
        /// Invalidates all compilations of the given template.
        /// WARNING: leads to memory leaks
        /// </summary>
        /// <param name="templateKey"></param>
        public void InvalidateCache(ITemplateKey templateKey)
        {
            var uniqueKey = templateKey.GetUniqueKeyString();
            ConcurrentDictionary <Type, ICompiledTemplate> dict;

            _cache.TryRemove(uniqueKey, out dict);
        }
Beispiel #2
0
        private void CacheTemplateHelper(ICompiledTemplate template, ITemplateKey templateKey, Type modelTypeKey)
        {
            var uniqueKey = templateKey.GetUniqueKeyString();

            _cache.AddOrUpdate(uniqueKey, key =>
            {
                // new item added
                _assemblies.Add(template.TemplateAssembly);
                var dict = new ConcurrentDictionary <Type, ICompiledTemplate>();
                dict.AddOrUpdate(modelTypeKey, template, (t, old) => {
                    throw new Exception("Expected the dictionary to be empty.");
                });
                return(dict);
            }, (key, dict) =>
            {
                dict.AddOrUpdate(modelTypeKey, t =>
                {
                    // new item added (template was not compiled with the given type).
                    _assemblies.Add(template.TemplateAssembly);
                    return(template);
                }, (t, old) =>
                {
                    // item was already added before
                    return(template);
                });
                return(dict);
            });
        }
 private void CacheTemplateHelper(ICompiledTemplate template, ITemplateKey templateKey, Type modelTypeKey)
 {
     var uniqueKey = templateKey.GetUniqueKeyString();
     _cache.AddOrUpdate(uniqueKey, key =>
     {
         // new item added
         _assemblies.Add(template.TemplateAssembly);
         var dict = new ConcurrentDictionary<Type, ICompiledTemplate>();
         dict.AddOrUpdate(modelTypeKey, template, (t, old) => {
             throw new Exception("Expected the dictionary to be empty."); });
         return dict;
     }, (key, dict) =>
     {
         dict.AddOrUpdate(modelTypeKey, t =>
         {
             // new item added (template was not compiled with the given type).
             _assemblies.Add(template.TemplateAssembly);
             return template;
         }, (t, old) =>
         {
             // item was already added before
             return template;
         });
         return dict;
     });
 }
Beispiel #4
0
        /// <summary>
        /// 缓存模板助手
        /// </summary>
        /// <param name="template">模板</param>
        /// <param name="templateKey">模板键</param>
        /// <param name="modelTypeKey">模板类型键</param>
        private void CacheTemplateHelper(ICompiledTemplate template, ITemplateKey templateKey, Type modelTypeKey)
        {
            var uniqueKey = templateKey.GetUniqueKeyString();

            _cache.AddOrUpdate(uniqueKey, key =>
            {
                // 添加新项
                _assemblies.Add(template.TemplateAssembly);
                var dict = new ConcurrentDictionary <Type, ICompiledTemplate>();
                dict.AddOrUpdate(modelTypeKey, template, (t, old) => template);
                return(dict);
            }, (key, dict) =>
            {
                dict.AddOrUpdate(modelTypeKey, t =>
                {
                    // 添加新条目(模板没有编译给定类型)
                    _assemblies.Add(template.TemplateAssembly);
                    return(template);
                }, (t, old) =>
                {
                    // 项目已经添加
                    return(template);
                });
                return(dict);
            });
        }
Beispiel #5
0
        /// <summary>
        /// Try to retrieve a template from the cache. See <see cref="ICachingProvider.TryRetrieveTemplate"/>.
        /// </summary>
        /// <param name="templateKey"></param>
        /// <param name="modelType"></param>
        /// <param name="compiledTemplate"></param>
        /// <returns></returns>
        public bool TryRetrieveTemplate(ITemplateKey templateKey, Type modelType, out ICompiledTemplate compiledTemplate)
        {
            compiledTemplate = null;
            if (templateKey == null)
            {
                return(false);
            }
            var uniqueKey = templateKey.GetUniqueKeyString();

            if (string.IsNullOrEmpty(uniqueKey))
            {
                return(false);
            }
            var modelTypeKey = GetModelTypeKey(modelType);
            ConcurrentDictionary <Type, ICompiledTemplate> dict;

            if (!_cache.TryGetValue(uniqueKey, out dict))
            {
                return(false);
            }
            if (modelTypeKey == null)
            {
                return(false);
            }
            return(dict.TryGetValue(modelTypeKey, out compiledTemplate));
        }
Beispiel #6
0
        /// <summary>
        /// 尝试检索模板
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="modelType">模型名称</param>
        /// <param name="template">模板</param>
        /// <returns>检索到返回true,否则返回false</returns>
        public bool TryRetrieveTemplate(ITemplateKey key, Type modelType, out ICompiledTemplate template)
        {
            template = null;
            var uniqueKey    = key.GetUniqueKeyString();
            var modelTypeKey = GetModelTypeKey(modelType);

            if (!_cache.TryGetValue(uniqueKey, out ConcurrentDictionary <Type, ICompiledTemplate> dict))
            {
                return(false);
            }
            return(dict.TryGetValue(modelTypeKey, out template));
        }
Beispiel #7
0
        /// <summary>
        /// Invalidates the compilation of the given template with the given model-type.
        /// WARNING: leads to memory leaks
        /// </summary>
        /// <param name="templateKey"></param>
        /// <param name="modelType"></param>
        public void InvalidateCacheOfType(ITemplateKey templateKey, Type modelType)
        {
            var uniqueKey    = templateKey.GetUniqueKeyString();
            var modelTypeKey = GetModelTypeKey(modelType);
            ConcurrentDictionary <Type, ICompiledTemplate> dict;

            if (!_cache.TryGetValue(uniqueKey, out dict))
            {
                // not cached
                return;
            }
            ICompiledTemplate c;

            dict.TryRemove(modelTypeKey, out c);
        }
Beispiel #8
0
        public static bool Remove(ITemplateKey templateKey)
        {
            if (templateKey == null)
            {
                return(false);
            }
            var uniqueKey = templateKey.GetUniqueKeyString();

            if (string.IsNullOrEmpty(uniqueKey))
            {
                return(false);
            }
            ConcurrentDictionary <Type, ICompiledTemplate> dict;

            if (_cache.TryRemove(uniqueKey, out dict))
            {
                return(true);
            }

            return(false);
        }
Beispiel #9
0
        /// <summary>
        /// Tries to resolve the template.
        /// When the cache misses we either throw an exception or compile the template.
        /// Otherwise the result is returned.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="modelType"></param>
        /// <param name="compileOnCacheMiss"></param>
        /// <returns></returns>
        internal ICompiledTemplate GetCompiledTemplate(ITemplateKey key, Type modelType, bool compileOnCacheMiss)
        {
            ICompiledTemplate template;

            if (!_config.CachingProvider.TryRetrieveTemplate(key, modelType, out template))
            {
                if (compileOnCacheMiss)
                {
                    template = CompileAndCacheInternal(key, modelType);
                }
                else
                {
                    throw new InvalidOperationException("No template exists with key '" + key.GetUniqueKeyString() + "'");
                }
            }
            return(template);
        }
        /// <summary>
        /// Tries to resolve the template.
        /// When the cache misses we either throw an exception or compile the template.
        /// Otherwise the result is returned.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="modelType"></param>
        /// <param name="compileOnCacheMiss"></param>
        /// <returns></returns>
        internal ICompiledTemplate GetCompiledTemplate(ITemplateKey key, Type modelType, bool compileOnCacheMiss)
        {
            ICompiledTemplate template;

            if (!_config.CachingProvider.TryRetrieveTemplate(key, modelType, out template))
            {
                if (compileOnCacheMiss)
                {
                    template = CompileAndCacheInternal(key, modelType);
                }
                else
                {
                    throw new InvalidOperationException("No template exists with key '" + key.GetUniqueKeyString() + "'");
                }
            }
            return template;
        }
 /// <summary>
 /// Try to retrieve a template from the cache. See <see cref="ICachingProvider.TryRetrieveTemplate"/>.
 /// </summary>
 /// <param name="templateKey"></param>
 /// <param name="modelType"></param>
 /// <param name="compiledTemplate"></param>
 /// <returns></returns>
 public bool TryRetrieveTemplate(ITemplateKey templateKey, Type modelType, out ICompiledTemplate compiledTemplate)
 {
     compiledTemplate = null;
     var uniqueKey = templateKey.GetUniqueKeyString();
     var modelTypeKey = GetModelTypeKey(modelType);
     ConcurrentDictionary<Type, ICompiledTemplate> dict;
     if (!_cache.TryGetValue(uniqueKey, out dict))
     {
         return false;
     }
     return dict.TryGetValue(modelTypeKey, out compiledTemplate);
 }
Beispiel #12
0
        /// <summary>
        /// 缓存失效
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>设置成功返回true, 失败返回false</returns>
        internal bool InvalidateCache(ITemplateKey key)
        {
            var uniqueKey = key.GetUniqueKeyString();

            return(InvalidateCache(uniqueKey));
        }
Beispiel #13
0
 public ITemplateSource Resolve(ITemplateKey key)
 {
     return(new PackageTemplateSource(new FileInfo(key.GetUniqueKeyString())));
 }
 /// <summary>
 /// Invalidates the compilation of the given template with the given model-type.
 /// WARNING: leads to memory leaks
 /// </summary>
 /// <param name="templateKey"></param>
 /// <param name="modelType"></param>
 public void InvalidateCacheOfType(ITemplateKey templateKey, Type modelType)
 {
     var uniqueKey = templateKey.GetUniqueKeyString();
     var modelTypeKey = GetModelTypeKey(modelType);
     ConcurrentDictionary<Type, ICompiledTemplate> dict;
     if (!_cache.TryGetValue(uniqueKey, out dict))
     {
         // not cached
         return;
     }
     ICompiledTemplate c;
     dict.TryRemove(modelTypeKey, out c);
 }
 /// <summary>
 /// Invalidates all compilations of the given template.
 /// WARNING: leads to memory leaks
 /// </summary>
 /// <param name="templateKey"></param>
 public void InvalidateCache(ITemplateKey templateKey)
 {
     var uniqueKey = templateKey.GetUniqueKeyString();
     ConcurrentDictionary<Type, ICompiledTemplate> dict;
     _cache.TryRemove(uniqueKey, out dict);
 }