性能优化之缓存--文件缓存

性能优化之缓存--文件缓存

一、文件缓存介绍:

  1. 文件缓存就是把文件存储在缓存中,这次主要介绍的是通过chche进行缓存文件。

  2. 直接看看代码吧:具体如下:

       string filePath = Request.MapPath("File.txt");
            if (Cache["fileContent"] == null)
            {
                //文件缓存依赖.
                CacheDependency cDep = new CacheDependency(filePath);
                string fileContent = File.ReadAllText(filePath);
                Cache.Insert("fileContent", fileContent, cDep);
                Response.Write("数据来自文件");
            }
            else
            {
                Response.Write("数据来自缓存:"+Cache["fileContent"].ToString());
            }

  3. 嗯,其实还是很简单的,拿过去用就行,注意,cache是应用于web中,所以需要导入如下:

using System.Web.Caching;

  4. 欢迎关注交流学习。

posted @ 2020-10-02 00:09  锦大大的博客呀!  阅读(453)  评论(0编辑  收藏  举报