开发者

Help with simple C# ASP.net output cache

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</li开发者_StackOverflow中文版nk>\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.


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:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜