既存の CDN を変更しようとしています。私がやろうとしているのは、短いキャッシュ時間を作成し、条件付き GET を使用してファイルが更新されたかどうかを確認することです。
最終更新日を設定して応答ヘッダーで確認しているにもかかわらず、後続の get 要求で If-Modified-Since ヘッダーが返されていないため、私は髪を引き裂いています。最初は、ローカルの開発環境か、Fiddler をテスト用のプロキシとして使用していたのではないかと考え、QA サーバーにデプロイしました。しかし、Firebug で見ていることは、私がしていることとは大きく異なります。最後に変更された日付が表示されます。何らかの理由でキャッシュ コントロールがプライベートに設定されています。ヘッダーの出力キャッシュをクリアしました。IIS 7.5 が書き込むように設定されている唯一のヘッダーは、HTTP キープアライブを有効にすることです。コードによって駆動される必要があります。
これは非常に簡単なことのように思えましたが、私は一日中ヘッダーを追加したり削除したりしていましたが、うまくいきませんでした。global.asax と他の場所をチェックしました (私はアプリを作成していないので、隠された驚きを探していて困惑しています。以下は現在のコードと要求ヘッダーと応答ヘッダーです。テストのためだけに有効期限を 30 秒に設定しています。私はいくつかのサンプルを見てきましたが、自分が何か違うことをしているとは思いませんが、単にうまくいきません。
応答ヘッダービューのソース Cache-Control プライベート、max-age=30 コンテンツの長さ 597353 コンテンツタイプの画像/jpg 日付 2013 年 9 月 3 日 21:33:55 GMT 有効期限 2013 年 9 月 3 日 21:34:25 GMT 最終更新日 2013 年 9 月 3 日 21:33:55 GMT サーバー Microsoft-IIS/7.5 X-AspNet-バージョン 4.0.30319 X-AspNetMvc-バージョン 3.0 X-Powered-By ASP.NET Request Headersソースを見る text/html、application/xhtml+xml、application/xml を受け入れる;q=0.9,*/*;q=0.8 Accept-Encoding gzip、デフレート Accept-Language en-US,en;q=0.5 接続キープアライブ クッキー __utma=1.759556114.1354835397.1377631052.1377732484.36; __utmz=1.1354835397.1.1.utmcsr=(直接)|utmccn=(直接)|utmcmd=(なし) ホスト hqat4app1 ユーザーエージェント Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetLastModified(DateTime.Now);
return new FileContentResult(fileContents, contentType);
関連するコードは次のとおりです。
public ActionResult Resize(int id, int size, bool grayscale)
{
_logger.Debug(() => string.Format("Resize {0} {1} {2}", id, size, grayscale));
string imageFileName = null;
if (id > 0)
using (new UnitOfWorkScope())
imageFileName = RepositoryFactory.CreateReadOnly<Image>().Where(o => o.Id == id).Select(o => o.FileName).SingleOrDefault();
CacheImageSize(id, size);
if (!ImageWasModified(imageFileName))
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
Response.StatusCode = (int)HttpStatusCode.NotModified;
Response.Status = "304 Not Modified";
return new HttpStatusCodeResult((int)HttpStatusCode.NotModified, "Not-Modified");
}
byte[] fileContents;
if (ShouldReturnDefaultImage(imageFileName))
fileContents = GetDefaultImageContents(size, grayscale);
else
{
bool foundImageFile;
fileContents = GetImageContents(id, size, grayscale, imageFileName, out foundImageFile);
if (!foundImageFile)
{
// No file found, clear cache, disable output cache
//ClearOutputAndRuntimeCacheForImage(id, grayscale);
//Response.DisableKernelCache();
}
}
string contentType = GetBestContentType(imageFileName);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetLastModified(DateTime.Now);
return new FileContentResult(fileContents, contentType);
}
private bool ImageWasModified(string fileName)
{
bool foundImageFile;
string filePath = GetFileOrDefaultPath(fileName, out foundImageFile);
if (foundImageFile)
{
string header = Request.Headers["If-Modified-Since"];
if(!string.IsNullOrEmpty(header))
{
DateTime isModifiedSince;
if (DateTime.TryParse(header, out isModifiedSince))
{
return isModifiedSince < System.IO.File.GetLastWriteTime(filePath);
}
}
}
return true;
}