2011-04-13 26 views
1

这不会给出想要的结果,如果我在数据库中添加更多记录,它们会立即显示在RSS提要上。简单的C#ASP.net输出缓存帮助

不好意思,刚才是完全清楚,我期待:

  • 我运行页面
  • 我看到的结果
  • 我在DB
  • 添加一条记录它不在加载缓存版本时出现在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; 
     } 
    } 

} 

我试着将它设置为大数字。我不会在请求之间修改源代码。

+2

您已省略:1.所需的结果。 2.一个问题。 – Oded 2011-04-13 18:58:48

+0

@Oded我以为我确实说得很清楚,不过我在开头还是更详细地加入了 – 2011-04-13 19:02:20

回答

2

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

question可能会帮助: