asp.net自定义输出缓存

ha5z0ras  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(277)

我有一个高流量的电子商务应用程序(net mvc)我使用redis缓存并编写自定义输出缓存过滤器。我的目的是在发送到浏览器之前更改一些内容。我的意思是我需要动态内容(我使用了油炸圈饼缓存,但在高流量下它没有像我预期的那样工作,我决定自己写一个)
它是从outputcacheattribute继承的。我重写onresultexecuted方法,并可以根据需要更改内容。你可以看看我的密码。
但有一个问题,myoutputcacheattribute只运行一次。当我刷新页面时,调试器只命中了一个断点。它没有第二次击中我的断点。
我想知道outputcacheattribute或outputcacheattribute中是否有内部缓存?我能做什么?

[MyOutputCache(Duration = int.MaxValue, VaryByCustom = "none",Location = System.Web.UI.OutputCacheLocation.Server, NoStore = true)]
public ActionResult Index()
   ...
}

public class MyOutputCacheAttribute : OutputCacheAttribute
{

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        var context = filterContext.HttpContext;
        var response = context.Response;
        response.Filter = new CaptureResponse(response.Filter, context);
    }
}

public class CaptureResponse : MemoryStream
{
    private readonly Stream _stream;
    private readonly HttpContextBase _context;

    public CaptureResponse(Stream stream, HttpContextBase context)
    {
        _stream = stream;
        _context = context;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        var html = Encoding.UTF8.GetString(buffer);

        //I change my place holder with my dynamic content 
        html = html.Replace("MyOldContent", "Dynamic Content" + DateTime.Now);

        byte[] newBuffer = Encoding.UTF8.GetBytes(html);

        _stream.Write(newBuffer, offset, newBuffer.Length);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题