データベースキャッシング機能を有効にする。データベース名が「TestDB 」、キャッシュ機能を有効にしたいテーブル名が「Table1 」である場合、Visual Studio 2008コマンドプロンプトで下記を実行する。
aspnet_regsql.exe -S .\SQLEXPRESS -E -d TestDB -ed
aspnet_regsql.exe -S .\SQLEXPRESS -E -d TestDB -t Table1 -et
Web.configの<system.web>内に下記を設定する。DBへの接続文字列が「TestDBConn」の場合。varyByParam=”*”を指定することで、パラメータごとにCacheが作られるようになる。sqlDependencyを指定することで、指定したDBのテーブルが更新されるとキャッシュが破棄される。
<caching>
<sqlCacheDependency enabled=”true” pollTime=”5000″>
<databases>
<add name=”TestDB” connectionStringName=”TestDBConn” />
</databases>
</sqlCacheDependency>
<outputCacheSettings>
<outputCacheProfiles>
<add name=”TestCache” enabled=”true” duration=”3600″ varyByParam=”*” sqlDependency=”TestDB :Table1″ />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Asp.Net MVCのControllerクラスのメソッドでOutputCache属性を使ってキャッシュプロファイルを設定する。
[HandleError]
public class TestController : Controller
{
[OutputCache(CacheProfile = "TestCache")]
public ActionResult Index(string id)
{
}