6

Microsoft の MVC3 フレームワークで OutputCache 属性を使用できません。

特定のmanufacturerIdに基づいて製品のリストを取得するために、AJAX呼び出しの一部として使用できる次のコントローラアクションを想像してください:

public JsonResult GetProducts(long manufacturerId)
{
    return Json(this.CreateProductList(manufacturerId), JsonRequestBehavior.AllowGet);
}

この結果をサーバーにキャッシュして、過剰なデータベース クエリを作成しないようにします。属性を次のように構成することで、これを実現できます。

[OutputCache(Duration = 3600, Location = OutputCacheLocation.Server, VaryByParam = "manufacturerId")]

これは期待どおりに機能します。ブラウザは最初のリクエストを作成し、サーバーに結果を作成してキャッシュさせます。同じまたは別のブラウザからの後続のリクエストは、キャッシュされたバージョンを取得します。

しかし...ブラウザがこれらの結果をローカルにキャッシュすることも望んでいます。最初にメーカー X でフィルタリングし、次に Y が X に戻った場合、X の製品に対して別のリクエストを作成したくありません。キャッシュされたバージョンだけを使用したいのです。

OutputCache を次のように変更することで、これを実現できます。

[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)]

ここに質問があります: 両方の動作セットを持つことができるように、これらをどのように組み合わせるのですか? Location を ServerAndClient に設定しようとしましたが、これにより Location が Server の場合と同じように動作しました。

この問題は、ServerAndClient で取得した "Vary: *" 応答ヘッダーに関係していると確信していますが、それを取り除く方法がわかりません。

私の意図の合理性についてのコメントを歓迎します - 期待した結果が得られないという事実は、どこかで根本的な誤解があるのではないかと考えさせます...

どうもありがとう。

PS: これはローカル開発環境、VS2010 の IIS Express にあります。

4

1 に答える 1

8

You can use OutputCacheLocation.Any which specifies

The output cache can be located on the browser client (where the request originated), on a proxy server (or any other server) participating in the request, or on the server where the request was processed. This value corresponds to the HttpCacheability.Public enumeration value.

You may want to also set Cache-control public to in the HTTP header for these requests.

Edit

It seems, depending on your .Net version of the web server you may need to include Response.Cache.SetOmitVaryStar(true); within your controller action to remove the Vary * headers as you suggest.

Details of the reason why in .Net 4 breaking changes release notes.

于 2012-08-21T09:55:51.977 に答える