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

时间:2011-04-13 18:56:06

标签: c# asp.net rss outputcache

如果我在数据库中添加更多记录,它们会立即显示在RSS源上,这不会产生预期的结果。

对不起,为了完全清楚,我期待着:

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

此时,新记录会立即显示在Feed中。

<%@ 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;
        }
    }

}

我也试过将它设置为一个大数字。我也没有在请求之间修改源代码。

1 个答案:

答案 0 :(得分:2)

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

question可能有所帮助: