MVC緩衝摘要

來源:互聯網
上載者:User

標籤:

   每當來到園子就會看到好多的系列文章,看的是眼花繚亂,不知所措,究其緣由就是自己學的東西太少了!!你不學,意味著你就漸漸的面臨落後,,,活到老學到老。學習是一個 習慣。。。

  看了大牛李林峰的園子講述了對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(小寫)。

根據提示完成啟用後,當更改資料庫時。緩衝依賴的時間也會更新。

引文:李林峰的園子

MVC緩衝摘要

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.