我們知道
針對上述情況
要實現自定義輸出緩存提供程序
代碼清單
using System;
using System
using System
using System
using System
using System
using System
using System
namespace _
{
public class MyOutputCacheProvider : OutputCacheProvider
{
private Timer timer;
private string cachePath =
private ConcurrentDictionary<string
cacheExpireList;
private const string KEY_PREFIX =
public MyOutputCacheProvider()
{
cacheExpireList =
new ConcurrentDictionary<string
timer = new Timer(
timer
{
var discardedList = from cacheItem in cacheExpireList
where cacheItem
select cacheItem;
foreach (var discarded in discardedList)
{
Remove(discarded
DateTime discardedDate;
cacheExpireList
out discardedDate);
}
};
timer
}
/// <summary>
/// 添加緩存
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <returns>返回緩存值</returns>
public override object Add(string key
DateTime utcExpiry)
{
FileStream fs = new FileStream(
String
FileMode
BinaryFormatter formatter = new BinaryFormatter();
formatter
fs
cacheExpireList
return entry;
}
/// <summary>
/// 獲得緩存值
/// </summary>
/// <param name=
/// <returns>緩存值</returns>
public override object Get(string key)
{
string path =
String
if (File
{
FileStream fs = new FileStream(
path
BinaryFormatter formatter = new BinaryFormatter();
object result = formatter
fs
return result;
}
else
{
return null;
}
}
/// <summary>
/// 根據鍵移除緩存
/// </summary>
/// <param name=
public override void Remove(string key)
{
string path =
String
if (File
{
File
}
}
/// <summary>
/// 設置緩存
/// </summary>
/// <param name=
/// <param name=
/// <param name=
public override void Set(string key
DateTime utcExpiry)
{
string path =
String
FileStream fs = new FileStream(
path
BinaryFormatter formatter = new BinaryFormatter();
formatter
fs
cacheExpireList
}
}
}
在MyOutputCacheProvider類中
定義好MyOutputCacheProvider類之後
<caching>
<outputCache defaultProvider=
<providers>
<add name=
type=
</providers>
</outputCache>
</caching>
默認情況下
<caching>
<outputCache defaultProvider=
<providers>
<add name=
type=
</providers>
</outputCache>
</caching>
此外
下面
<%@ Control Language=
CodeBehind=
Inherits=
<%@ OutputCache Duration=
<asp:Label ID=
MyOutputCacheProviderUserControl
public partial class MyOutputCacheProviderUserControl :System
{
protected void Page_Load(object sender
{
this
}
}
定義好MyOutputCacheProviderUserControl用戶控件之後
<%@ Page Language=
CodeBehind=
Inherits=
<%@ Register src=
tagname=
<!DOCTYPE html PUBLIC
<html xmlns=
<head runat=
<title></title>
</head>
<body>
<form id=
<div>
<asp:Button ID=
<br />
MyOutputCacheProviderUserControl
<uc
<br />
MyOutputCacheProviderWebForm
<asp:Label ID=
</div>
</form>
</body>
</html>
MyOutputCacheProviderWebForm
public partial class MyOutputCacheProviderWebForm :
System
{
protected void Page_Load(object sender
{
this
}
}
運行MyOutputCacheProviderWebForm
圖
打開
圖
圖
這裡需要注意的是
雖然你不能夠以聲明的方式指定@ OutputCache指令的ProviderName屬性
public override string GetOutputCacheProviderName(HttpContext context)
{
if (context
{
return
}
else
{
return base
}
}
利用ASP
From:http://tw.wingwit.com/Article/program/ASP/201311/21654.html