65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET 输出缓存提供程序

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2010年7月18日

CPOL

2分钟阅读

viewsIcon

19397

ASP.NET 4 随附的一个新特性是用于缓存目的的新提供程序。 在本文中,我将解释其中之一 – OutputCacheProvider。

ASP.NET 4 随附的一个新特性是用于缓存目的的新提供程序。 在本文中,我将解释其中之一 – OutputCacheProvider

OutputCacheProvider

ASP.NET 4 之前,输出缓存机制是作为内存缓存实现的,我们无法做任何事情来改变它的行为。 如果我们希望使用分布式缓存,例如 AppFabric 缓存或我们自己的实现,我们无法实现它。 从 ASP.NET 4 开始,缓存现在遵循 ASP.NET 提供程序模型,这意味着我们可以创建自己的提供程序以使用缓存。 如果我们希望使用我们自己的输出缓存,我们所需要做的就是继承 System.Web.Caching 命名空间 中存在的 OutputCacheProvider 类。 在实现包含 AddGetRemoveSet 方法的相关 接口 后,我们可以将我们的提供程序插入到 web.config 文件中以使用它。

OutputCacheProvider 示例

如前所述,我们所需要做的就是继承 OutputCacheProvider 类。 以下代码示例是 OutputCacheProvider 的一个简单的内存实现

public class InMemoryOutputCacheProvider : OutputCacheProvider
{
  #region Members
 
  private Dictionary<string, InMemoryOutputCacheItem> _cache = 
      new Dictionary<string, InMemoryOutputCacheItem>();
  private readonly static object _syncLock = new object();
 
  #endregion
 
  #region Methods
 
  public override object Add(string key, object entry, DateTime utcExpiry)
  {
    Set(key, entry, utcExpiry);
    return entry;
  }
 
  public override object Get(string key)
  {
    InMemoryOutputCacheItem item = null;
    if (_cache.TryGetValue(key, out item))
    {
      if (item.UtcExpiry < DateTime.UtcNow)
      {
        Remove(key);
        return null;
      }
      return item.Value;
    }
    return null;
  }
 
  public override void Remove(string key)
  {
    InMemoryOutputCacheItem item = null;
    if (_cache.TryGetValue(key, out item))
    {
      _cache.Remove(key);
    }
  }
 
  public override void Set(string key, object entry, DateTime utcExpiry)
  {
    var item = new InMemoryOutputCacheItem(entry, utcExpiry);
    lock (_syncLock)
    {
      if (_cache.ContainsKey(key))
      {
        _cache[key] = item;
      }
      else
      {
        _cache.Add(key, item);
      }
    }
  }
 
  #endregion
}

在代码中,我将缓存实现为一个内存字典。 实现非常简单,基于一个如下所示的 item 类:

public class InMemoryOutputCacheItem
{
  #region Members
 
  public DateTime UtcExpiry { get; set; }
  public object Value { get; set; }
 
  #endregion
 
  #region Ctor
 
  public InMemoryOutputCacheItem(object value, DateTime utcExpiry)
  {
    Value = value;
    UtcExpiry = utcExpiry;
  }
 
  #endregion
}

配置 OutputCacheProvider

为了使用之前的输出缓存实现,我们需要将其插入到应用程序的 web.config 文件中。 在 system.web 元素下,我们需要添加 caching 元素。 在 caching 元素内部,我们添加一个 outputCache 元素并添加我们创建的提供程序。 以下示例显示了如何操作:

<?xml version="1.0"?>
<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <compilation debug="true" targetFramework="4.0">
        </compilation>
    <caching>
      <outputCache defaultProvider="InMemory">
        <providers>
          <add name="InMemory" type="InMemoryOutputCacheProvider"/>
        </providers>
      </outputCache>
    </caching>
        <authentication mode="Windows"/>
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
    </system.web>
</configuration>

检查实现

现在我们准备好测试实现。 以下网页包含一个标签控件,显示当前日期和时间(在代码隐藏文件中插入)。 如您所见,我添加了 OutputCache 页面指令到页面以测试提供程序

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
    Inherits="HttpModuleTestWeb.WebForm1" %>
<%@ OutputCache Duration="15" VaryByParam="*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblTime" runat="server" />
    </div>
    </form>
</body>
</html>

页面代码隐藏文件

public partial class WebForm1 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    lblTime.Text = DateTime.Now.ToString();
  }
}

摘要

ASP.NET 4 中,缓存现在是基于提供程序的。 这意味着我们可以将我们的实现插入到缓存中,包括输出缓存。 在本文中,我展示了一个如何用一个简单的内存实现替换输出缓存的示例。

© . All rights reserved.