eTag 可以像在 Rails 中一样用于 asp.net MVC 中的智能客户端缓存吗?

发布于 2024-08-05 09:27:30 字数 491 浏览 6 评论 0原文

我在此截屏视频中找到了 ¹在 Ruby on Rails 中,您可以执行以下操作: 更好的客户端缓存 ² 考虑 REST + 模型来填充电子标签。比渲染所有 http 正文更聪明,只有在完成所有这些之后,才像往常一样计算 eTag。

这个属性可以使客户端缓存在使用 GET 时更加面向模型,所以我认为这对于性能来说非常好。

我没有看到有人用 asp.net MVC 来做这个。它会像我看到这个人在 Rails 中所做的那样简单吗?

I've found in this screencast ¹ that you can do, in Ruby on Rails, a better client caching ² considering REST + model for filling eTag. Way more smart than render all the http body and only after all this, calculate the eTag, as usual.

This property can make the client caching more model-oriented when using GET, so I think this is great for performance.

I didn't see anywhere someone making this with asp.net MVC. Would it be as easy as I saw this guy doing in Rails?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2024-08-12 09:27:30

电子标签是一个 HTTP 概念,而不是与任何一种服务器端技术相关。我相信与 IIS/MVC 相比,RoR 可以更轻松地指示使用 eTag 缓存特定文件。

对于 IIS/MVC,您有两个设置响应标头(可以包括 eTag 设置)的选项:

  1. 在 IIS 内容过期设置对话框中。
  2. 设置 HTTP 处理程序或类似技术,以便您准确指定特定请求所需的响应标头。这是步骤 #1 的编程变体。如果您通过网络传输数据库内容,则这是必要的,在这种情况下,没有真正的文件可言。

如果还有其他我没有列出的方法可以实现这一点,我很想了解它们。

eTags are an HTTP concept more than they are related to any one server-side technology. I believe RoR may make it easier when compared to IIS/MVC to indicate that a particular file is cached using an eTag.

For IIS/MVC, you have two options of setting up response headers (which can include eTag settings):

  1. In the IIS Content Expiration settings dialog.
  2. Setting up HTTP Handlers or a similar technique so that you specify exactly which response headers you want for a particular request. This is a programmatic variation of step #1. It is necessary if you're streaming database content over the wire, in which case there isn't a real file to speak of.

If there are other ways of making this happen that I haven't listed, I would love to know about them.

ヤ经典坏疍 2024-08-12 09:27:30

使用 ActionFilterAttribute 通过过滤器更新响应

public class ETagAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Filter = new ETagFilter(filterContext.HttpContext.Response, filterContext.RequestContext.HttpContext.Request);
    }
}

public class ETagFilter : MemoryStream
{
    private HttpResponseBase _response = null;
    private HttpRequestBase _request;
    private Stream _filter = null;

    public ETagFilter(HttpResponseBase response, HttpRequestBase request)
    {
        _response = response;
        _request = request;
        _filter = response.Filter;
    }

    private string GetToken(Stream stream)
    {
        byte[] checksum = new byte[0];
        checksum = MD5.Create().ComputeHash(stream);
        return Convert.ToBase64String(checksum, 0, checksum.Length);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        byte[] data = new byte[count];
        Buffer.BlockCopy(buffer, offset, data, 0, count);
        var token = GetToken(new MemoryStream(data));

        string clientToken = _request.Headers["If-None-Match"];

        if (token != clientToken)
        {
            _response.Headers["ETag"] = token;
            _filter.Write(data, 0, count);
        }
        else
        {
            _response.SuppressContent = true;
            _response.StatusCode = 304;
            _response.StatusDescription = "Not Modified";
            _response.Headers["Content-Length"] = "0";
        }
    }
}


在 ASP.NET MVC 中创建 ETag 过滤器

Use ActionFilterAttribute to update response via filter

public class ETagAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Filter = new ETagFilter(filterContext.HttpContext.Response, filterContext.RequestContext.HttpContext.Request);
    }
}

public class ETagFilter : MemoryStream
{
    private HttpResponseBase _response = null;
    private HttpRequestBase _request;
    private Stream _filter = null;

    public ETagFilter(HttpResponseBase response, HttpRequestBase request)
    {
        _response = response;
        _request = request;
        _filter = response.Filter;
    }

    private string GetToken(Stream stream)
    {
        byte[] checksum = new byte[0];
        checksum = MD5.Create().ComputeHash(stream);
        return Convert.ToBase64String(checksum, 0, checksum.Length);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        byte[] data = new byte[count];
        Buffer.BlockCopy(buffer, offset, data, 0, count);
        var token = GetToken(new MemoryStream(data));

        string clientToken = _request.Headers["If-None-Match"];

        if (token != clientToken)
        {
            _response.Headers["ETag"] = token;
            _filter.Write(data, 0, count);
        }
        else
        {
            _response.SuppressContent = true;
            _response.StatusCode = 304;
            _response.StatusDescription = "Not Modified";
            _response.Headers["Content-Length"] = "0";
        }
    }
}

iriginal from:
Create ETag filter in ASP.NET MVC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文