帮助处理简单的 C# ASP.net 输出缓存

发布于 2024-11-01 19:58:53 字数 3755 浏览 0 评论 0原文

这不会给出预期的结果,如果我在数据库中添加更多记录,它们会立即出现在 RSS 提要上。

抱歉,为了完全清楚,我期待:

  • 我运行页面
  • 我看到结果
  • 我在数据库中添加一条记录
  • 它不会出现在提要上,因为它正在加载缓存版本
  • 缓存过期后出现在

它会在 新记录立即出现在提要中。

<%@ WebHandler Language="C#" Class="BlogRSS" %>

using System;
using System.Web;
using System.Web.UI;
using System.Linq;

public class BlogRSS : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        // Cache this
        OutputCacheParameters CacheSetting = new OutputCacheParameters();
        CacheSetting.Duration = 10;

        // Print out response
        context.Response.ContentType = "text/xml";
        context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        context.Response.Write("<rss xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\">\n\n");

        context.Response.Write("<channel>\n");
        context.Response.Write("\t<title>Scirra Blog</title>\n");   
        context.Response.Write("\t<link>" + Settings.MasterDomainRoot + "/Blog</link>\n");
        context.Response.Write("\t<description>Construct, Scirra and general game industry news - Scirra.com</description>\n");
        context.Response.Write("\t<language>en-us</language>\n\n");

        context.Response.Write("\t<image>\n");
        context.Response.Write("\t\t<title>Scirra Blog</title>\n");         
        context.Response.Write("\t\t<url>" + Settings.MasterDomainRoot + "/Images/blog-icon-small.png</url>\n");    
        context.Response.Write("\t\t<width>100</width>\n"); 
        context.Response.Write("\t\t<height>91</height>\n");        
        context.Response.Write("\t\t<height>91</height>\n");
        context.Response.Write("\t\t<link>" + Settings.MasterDomainRoot + "/Blog</link>\n");            
        context.Response.Write("\t</image>\n\n");

        context.Response.Write("\t<xhtml:meta xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" />\n\n");

        // Get all blog entries
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var q = (from Entry in db.tblBlogEntries orderby Entry.date descending select new { Entry.date, Entry.description, Entry.ID, Entry.title });
            foreach (var Rec in q)
            {
                PrintEntryXML(Rec.ID, Rec.title, Rec.description, Rec.date.Value);
            }
        }

        context.Response.Write("</channel>\n");
        context.Response.Write("</rss>\n");

    }

    /// <summary>
    /// Prints out an item
    /// </summary>
    public static void PrintEntryXML(int EntryID, string Title, string Description, DateTime PublishDate)
    {
        HttpContext.Current.Response.Write("\t<item>\n");

        HttpContext.Current.Response.Write("\t\t<title>" + Title + "</title>\n");
        HttpContext.Current.Response.Write("\t\t<link>" + Settings.MasterDomainRoot + "/Blog/" + EntryID + "/" + SEO.FriendlyURL(Title) + "</link>\n");
        HttpContext.Current.Response.Write("\t\t<description>\n");
        HttpContext.Current.Response.Write("\t\t\t" + Description + "\n");
        HttpContext.Current.Response.Write("\t\t</description>\n");
        HttpContext.Current.Response.Write("\t\t<pubDate>" + CommonFunctions.PubDate(PublishDate) + "</pubDate>\n");

        HttpContext.Current.Response.Write("\t</item>\n\n");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

我也尝试过将其设置为一个大数字。我也不会修改请求之间的源。

This doesn't give desired results, if I add more records in the database they appear immediately on the RSS feed.

Sorry, just to be entirely clear, I'm expecting:

  • I run the page
  • I see results
  • I add a record in the DB
  • It doesn't appear on the feed as it's loading a cached version
  • It appears later when the cache has expired

At the moment new records appear in the feed immediately.

<%@ WebHandler Language="C#" Class="BlogRSS" %>

using System;
using System.Web;
using System.Web.UI;
using System.Linq;

public class BlogRSS : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        // Cache this
        OutputCacheParameters CacheSetting = new OutputCacheParameters();
        CacheSetting.Duration = 10;

        // Print out response
        context.Response.ContentType = "text/xml";
        context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        context.Response.Write("<rss xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\">\n\n");

        context.Response.Write("<channel>\n");
        context.Response.Write("\t<title>Scirra Blog</title>\n");   
        context.Response.Write("\t<link>" + Settings.MasterDomainRoot + "/Blog</link>\n");
        context.Response.Write("\t<description>Construct, Scirra and general game industry news - Scirra.com</description>\n");
        context.Response.Write("\t<language>en-us</language>\n\n");

        context.Response.Write("\t<image>\n");
        context.Response.Write("\t\t<title>Scirra Blog</title>\n");         
        context.Response.Write("\t\t<url>" + Settings.MasterDomainRoot + "/Images/blog-icon-small.png</url>\n");    
        context.Response.Write("\t\t<width>100</width>\n"); 
        context.Response.Write("\t\t<height>91</height>\n");        
        context.Response.Write("\t\t<height>91</height>\n");
        context.Response.Write("\t\t<link>" + Settings.MasterDomainRoot + "/Blog</link>\n");            
        context.Response.Write("\t</image>\n\n");

        context.Response.Write("\t<xhtml:meta xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" />\n\n");

        // Get all blog entries
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var q = (from Entry in db.tblBlogEntries orderby Entry.date descending select new { Entry.date, Entry.description, Entry.ID, Entry.title });
            foreach (var Rec in q)
            {
                PrintEntryXML(Rec.ID, Rec.title, Rec.description, Rec.date.Value);
            }
        }

        context.Response.Write("</channel>\n");
        context.Response.Write("</rss>\n");

    }

    /// <summary>
    /// Prints out an item
    /// </summary>
    public static void PrintEntryXML(int EntryID, string Title, string Description, DateTime PublishDate)
    {
        HttpContext.Current.Response.Write("\t<item>\n");

        HttpContext.Current.Response.Write("\t\t<title>" + Title + "</title>\n");
        HttpContext.Current.Response.Write("\t\t<link>" + Settings.MasterDomainRoot + "/Blog/" + EntryID + "/" + SEO.FriendlyURL(Title) + "</link>\n");
        HttpContext.Current.Response.Write("\t\t<description>\n");
        HttpContext.Current.Response.Write("\t\t\t" + Description + "\n");
        HttpContext.Current.Response.Write("\t\t</description>\n");
        HttpContext.Current.Response.Write("\t\t<pubDate>" + CommonFunctions.PubDate(PublishDate) + "</pubDate>\n");

        HttpContext.Current.Response.Write("\t</item>\n\n");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

I've tried setting it to a big number as well. I'm not modifying source between requests either.

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

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

发布评论

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

评论(1

秋意浓 2024-11-08 19:58:53

您是否错过了对 InitOutputCache 的调用?我不确定这在处理程序中如何工作,但我认为如果这是一个页面,您将需要它。

这个问题可能会有所帮助:

Are you missing a call to InitOutputCache? I'm not sure how this works in a handler, but I think you would need that if this were a page.

This question may help:

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