帮助实现简单的c# ASP.net输出缓存

本文关键字:net 输出 缓存 ASP 实现 简单 帮助 | 更新日期: 2023-09-27 17:49:58

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

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

  • 我运行页面
  • 我看到结果
  • 我在DB中添加了一个记录
  • 它没有出现在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;
        }
    }
}

我也试过把它设置成一个大的数字。我也没有修改请求之间的源

帮助实现简单的c# ASP.net输出缓存

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