我编写了一个图像处理程序,它还通过在满足某些条件时发送 304 来实现客户端缓存。为了访问不同的照片或登录的人自己的照片,我们将处理程序按以下方式放在不同的页面上。
<img src="EmployeePhoto.ashx" />
<img src="EmployeePhoto.ashx?self" />
<img src="EmployeePhoto.ashx?default" />
问题是,如果我在图像缓存在页面上后第二次通过单击链接访问任何页面,它不会命中处理程序(调试器不会命中)。但是,如果我使用 F5 或刷新按钮刷新该页面,那么它只会触发处理程序并显示我想要显示的内容。
只有当我尝试使用附加的另一个参数(如“&date =”+ CurrentDate)来访问它时,它才会触发处理程序,但它会破坏缓存的目的。
<img src="EmployeePhoto.ashx?default&date=03/31/14 00:00:00" /> //or something like that
我知道代码没有问题,因为它在被击中时效果很好。无论如何,这是代码。
public void ProcessRequest(HttpContext context)
{
var isDefault = true;
var textIfModifiedSince = context.Request.Headers["If-Modified-Since"];
context.Response.ClearHeaders();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMonths(6));
try
{
var isOwn = false;
var user = /*Gets logged in user instance from service*/;
var employeeId = 0;
if (context.Request.QueryString.Count > 0)
{
if(context.Request.QueryString[0] == "self")
{
if (user != null)
{
employeeId = Convert.ToInt32(user.EmployeeId);
isOwn = true;
}
}
else if (!int.TryParse(context.Request.QueryString[0], out employeeId))
employeeId = 0;
}
else
{
if (user != null)
{
employeeId = Convert.ToInt32(user.EmployeeId);
isOwn = true;
}
}
if (user != null && employeeId != 0)
{
var employee = GetEmployee(employeeId);
if (
(!string.IsNullOrEmpty(textIfModifiedSince) && employee.Modify_Date == null)
|| !string.IsNullOrEmpty(textIfModifiedSince)
&& employee.Modify_Date != null
&& employee.Modify_Date <= Convert.ToDateTime(textIfModifiedSince).AddMinutes(1))
{
isDefault = false;
context.Response.Cache.SetLastModified(Convert.ToDateTime(textIfModifiedSince));
context.Response.Status = "304 Not Modified";
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
if (
employee != null
&& !string.IsNullOrEmpty(employee.Picture)
&& (isOwn || employee.LocationID != null)
&& (isOwn || HasRequiredRoles))
{
var path = context.Server.MapPath("~//" + EmployeePhotoPath);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var fileName = employee.Picture;
var destinationPath =
context.Server.MapPath("~//" + EmployeePhotoPath).ToString(CultureInfo.InvariantCulture);
if (File.Exists(destinationPath + fileName))
{
isDefault = false;
context.Response.Cache.SetLastModified(employee.Modify_Date > DateTime.Now ? DateTime.Now : (employee.Modify_Date ?? DateTime.Now));
context.Response.ContentType = GetContentType(employee.Picture);
context.Response.WriteFile(EmployeePhotoPath + employee.Picture);
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
isDefault = false;
DownloadFromSFTP(path, employee.Picture, user, employeeId.ToString(CultureInfo.InvariantCulture));
context.Response.Cache.SetLastModified(employee.Modify_Date > DateTime.Now ? DateTime.Now : (employee.Modify_Date ?? DateTime.Now));
context.Response.ContentType = GetContentType(employee.Picture);
context.Response.WriteFile(EmployeePhotoPath + employee.Picture);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
catch (Exception ex)
{
Log.Debug("Photo Handler Failed.", ex);
}
finally
{
if (isDefault)
{
if (!string.IsNullOrEmpty(textIfModifiedSince))
{
context.Response.Cache.SetLastModified(Convert.ToDateTime(textIfModifiedSince));
context.Response.Status = "304 Not Modified";
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
context.Response.Cache.SetLastModified(DateTime.Now);
context.Response.ContentType = GetContentType("images/avatar.gif");
context.Response.WriteFile("images/avatar.gif");
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
补充:在以下情况下会出现问题。
- 用户通过访问 EmployeePhoto.ashx?Eid=20349 访问图像
- 管理员更改 Eid=20349 的照片
- 用户再次访问该页面(不刷新,而是从某个链接访问该页面)
页面上的缓存图像带来了旧图像,如果它已经命中处理程序,那么情况已经在那里处理,但它没有并且页面显示缓存版本本身。我只在chrome上检查过。