Exemplo n.º 1
0
        /// <summary>
        /// 通过缓存解析模板
        /// </summary>
        /// <param name="content"></param>
        /// <param name="model"></param>
        /// <param name="cacheFileName"></param>
        /// <param name="builderAction"></param>
        /// <returns></returns>
        public async Task <string> RunCompileFromCachedAsync <T>(string content, T model, string cacheFileName = default, Action <IViewEngineOptionsBuilder> builderAction = null)
            where T : class, new()
        {
            var fileName = cacheFileName ?? MD5Encryption.Encrypt(content);

            IViewEngineTemplate <ViewEngineModel <T> > template;

            if (File.Exists(Penetrates.GetTemplateFileName(fileName)))
            {
                template = await ViewEngineTemplate <ViewEngineModel <T> > .LoadFromFileAsync(fileName);
            }
            else
            {
                template = await CompileAsync <ViewEngineModel <T> >(content, builderAction);

                await template.SaveToFileAsync(fileName);
            }

            var result = await template.RunAsync(u =>
            {
                u.Model = model;
            });

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 保存到文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task SaveToFileAsync(string fileName)
        {
            using var fileStream = new FileStream(
                      path: Penetrates.GetTemplateFileName(fileName),
                      mode: FileMode.OpenOrCreate,
                      access: FileAccess.Write,
                      share: FileShare.None,
                      bufferSize: 8192,
                      useAsync: true);

            await assemblyByteCode.CopyToAsync(fileStream);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 从文件中加载模板
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static async Task <IViewEngineTemplate> LoadFromFileAsync(string fileName)
        {
            using var memoryStream = new MemoryStream();

            using (var fileStream = new FileStream(
                       path: Penetrates.GetTemplateFileName(fileName),
                       mode: FileMode.Open,
                       access: FileAccess.Read,
                       share: FileShare.None,
                       bufferSize: 8192,
                       useAsync: true))
            {
                await fileStream.CopyToAsync(memoryStream);
            }

            return(new ViewEngineTemplate(memoryStream));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 通过缓存解析模板
        /// </summary>
        /// <param name="content"></param>
        /// <param name="model"></param>
        /// <param name="cacheFileName"></param>
        /// <param name="builderAction"></param>
        /// <returns></returns>
        public string RunCompileFromCached(string content, object model = null, string cacheFileName = default, Action <IViewEngineOptionsBuilder> builderAction = null)
        {
            var fileName = cacheFileName ?? MD5Encryption.Encrypt(content);

            IViewEngineTemplate template;

            if (File.Exists(Penetrates.GetTemplateFileName(fileName)))
            {
                template = ViewEngineTemplate.LoadFromFile(fileName);
            }
            else
            {
                template = Compile(content, builderAction);
                template.SaveToFile(fileName);
            }

            var result = template.Run(model);

            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 通过缓存解析模板
        /// </summary>
        /// <param name="content"></param>
        /// <param name="model"></param>
        /// <param name="cacheFile"></param>
        /// <param name="builderAction"></param>
        /// <returns></returns>
        public async Task <string> RunCompileFromCachedAsync(string content, object model = null, string cacheFile = default, Action <IViewEngineOptionsBuilder> builderAction = null)
        {
            var fileName = cacheFile ?? MD5Encryption.Encrypt(content);

            IViewEngineTemplate template;

            if (File.Exists(Penetrates.GetTemplateFileName(fileName)))
            {
                template = await ViewEngineTemplate.LoadFromFileAsync(fileName);
            }
            else
            {
                template = await CompileAsync(content, builderAction);

                await template.SaveToFileAsync(fileName);
            }

            var result = await template.RunAsync(model);

            return(result);
        }