zoukankan      html  css  js  c++  java
  • MVC缓存摘要

       每当来到园子就会看到好多的系列文章,看的是眼花缭乱,不知所措,究其缘由就是自己学的东西太少了!!你不学,意味着你就渐渐的面临落后,,,活到老学到老。学习是一个 习惯。。。

      看了大牛李林峰的园子讲述了对mvc缓存的简要应用。自己也在加深对mvc的了解。简要练习下:

     引用李林峰的园子:缓存是将信息(数据或页面)放在内存中以避免频繁的数据库存储或执行整个页面的生命周期,直到缓存的信息过期或依赖变更才再次从数据库中读取数据或重新执行页面的生命周期。在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法。场景:数据被频繁的使用,并且很少发生变化或对即时性的要求不高。

      第一、Controller控制器里面的缓存:影响类内所有的Action duration=10单位是秒,过期后再次触发更新缓存

    [OutputCache(Duration=10)]  
    public class StudentManagerController : Controller
    {
               private StudentEntities db = new StudentEntities();

    public ViewResult Index()
    {
        var stulist = db.stuList.Include(s => s.scClass);
        ViewBag.scClassID = new SelectList(db.scClassList, "ID", "className");
        return View(stulist.ToList());
    }

    }

     

      第二:Action动作缓存:

    public class StudentManagerController : Controller
    {
               private StudentEntities db = new StudentEntities();

    [OutputCache(Duration=10)]
    public ActionResult Create()
    {
         ViewBag.scClassID = new SelectList(db.scClassList, "ID", "className");
         return View();
    }

    }

     

          第三、在Config文件中配置缓存:当有很多Aontroller和Action加入缓存时,且参数不一致,再通过程序读取配置设置。

           配置文件需要在system.web的缓存节点上配置如下:

    <system.web>
       <caching>
          <outputCacheSettings>
             <outputCacheProfiles>
                 <add name="configcache" duration="20"/>
             </outputCacheProfiles>
          </outputCacheSettings>
       </caching>
    </system.web>

    读取配置文件中的缓存:如果都用了缓存Action缓存为主Controller缓存随后

    public class StudentManagerController : Controller
    {
               private StudentEntities db = new StudentEntities();

    [OutputCache(CacheProfile = "configcache")]
    public ActionResult Create()
    {
         ViewBag.scClassID = new SelectList(db.scClassList, "ID", "className");
         return View();
    }

    }

     

         第四、缓存依赖,即通过配置节点SqlDependency获取关联的数库和表实现缓存依赖:

             1. 在应用程序中调用节点内的名称Sqldepependencycache

    public class StudentManagerController : Controller
    {
               private StudentEntities db = new StudentEntities();

     

              [OutputCache(CacheProfile = "Sqldepependencycache")]
              public ActionResult Create()
             {
                 ViewBag.scClassID = new SelectList(db.scClassList, "ID", "className");
                 return View();
              }

    }

         2.在配置文件中添加数据库依赖:

    <system.web>
      <caching>
        <sqlCacheDependency>
            <databases>
                add name="Usercachedependencycache" connectionStringName="Conn" pollTime="40" />
            </databases>
       </sqlCacheDependency>
       <outputCacheSettings>
            <outputCacheProfiles>
                <add name="Sqldepependencycache" duration="20" sqlDependency="Usercachedependencycache:student"/>
            </outputCacheProfiles>
        </outputCacheSettings>
      </caching>
    </system.web>

    解读:

    * 因为依赖数据库,所有必须包含数据库连接节点connectionStrings;Conn是数据库连接字符串的名称。

    *. polltime="40"即监听数据库是否变化单位是毫秒。即40毫秒一个周期;

    *.  sqlDependency="Usercachedependencycache:student" 即数据库依赖节点的名称:student是表名注意是小写,多个的话就用逗号分隔开,别忘了也得写原来节点的名称Usercachedependencycache:student2.

     

    3.启用数据库的节点名称:

    把vs的命令窗口打开依次输入命令:asp_regsql -S localhost  -U sa -p 520488 -ed -d schoolDB -et -t student

    即数据库地址:localhost;用户:sa,密码:520488 数据库名称:schoolDB 表名:student(小写)。

    根据提示完成启用后,当更改数据库时。缓存依赖的时间也会更新。

    引文:李林峰的园子

  • 相关阅读:
    IIS无法显示 XML 页
    asp.net实现sql存取图片
    微软的面试题
    IIS配置.Net问题大全
    Asp.Net调用WebService
    生活本无常,前路更精彩
    【转载】碰到讨厌的老板怎么办
    xxxx不必xx尽,留些xxxx
    【BLOG】Mr梵谷
    机会,从来都是留给有准备的你
  • 原文地址:https://www.cnblogs.com/professional-NET/p/4805219.html
Copyright © 2011-2022 走看看