转载:缓存 Cache
/// <summary>
??????? /// 緩存函數(shù)
??????? /// </summary>
??????? /// <param name="page"></param>
??????? /// <param name="addMinutes">緩存的分鐘</param>
??????? /// <param name="key"></param>
??????? /// <param name="value"></param>
??????? private void CacheAdd(System.Web.UI.Page page, int addMinutes, string key, object value)
??????? {
??????????? DateTime absoluteExpiration = DateTime.Now.AddMinutes(addMinutes);
??????????? TimeSpan slidingExpiration = TimeSpan.FromMinutes(addMinutes);
??????????? page.Cache.Insert(key, value, null,
??????????? absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration,
??????????? System.Web.Caching.CacheItemPriority.High, null);
??????? }
ASP.NET 提供一個(gè)功能完整的緩存引擎,頁面可使用該引擎通過 HTTP 請(qǐng)求存儲(chǔ)和檢索任意對(duì)象.
緩存的生存期與應(yīng)用程序的生存期相同,也就是說,當(dāng)應(yīng)用程序重新啟動(dòng)時(shí),將重新創(chuàng)建緩存。
將數(shù)據(jù)添加到緩存中
1。通過指定其鍵和值將項(xiàng)添加到緩存中
Cache["txt"] = "a";
2.通過使用 Insert(重載Insert方法)方法將項(xiàng)添加到緩存中
Cache.Insert("txt", "a");
下列代碼顯示如何設(shè)置相對(duì)過期策略。它插入一個(gè)項(xiàng),該項(xiàng)自上次訪問后 10 分鐘過期。注意 DateTime.MaxValue 的使用,它表示此項(xiàng)沒有絕對(duì)過期策略。
DateTime absoluteExpiration=DateTime.MaxValue;
TimeSpan slidingExpiration=TimeSpan.FromMinutes(10);
Cache.Insert("txt", "a",null,
absoluteExpiration,slidingExpiration,
System.Web.Caching.CacheItemPriority.High,null);
3.使用 Add 方法將項(xiàng)添加到緩存中
Add 方法與 Insert 方法具有相同的簽名,但它返回表示您所添加項(xiàng)的對(duì)象
DateTime absoluteExpiration=DateTime.MaxValue;
TimeSpan slidingExpiration=TimeSpan.FromMinutes(10);
Object? Ojb=(string)Cache.Add("txt","a",null,
absoluteExpiration ,slidingExpiration ,
System.Web.Caching.CacheItemPriority.High,null);
string str=(string)Ojb ;
Response.Write(str);
結(jié)果顯示是"a"
Add 方法使用上沒有Insert 方法靈活,使用Add 方法時(shí)必須提供7個(gè)參數(shù),Insert 方法重載4次,我們可以根據(jù)需要選擇適當(dāng)重載方法
從緩存中取得數(shù)據(jù)
方式1:
string str=(string)Cache.Get("txt");
Response.Write(str);
方式2:
string str1=(string)Cache["txt1"];
Response.Write(str1);
查看Cache中所有數(shù)據(jù)
System.Text.StringBuilder sb=new System.Text.StringBuilder("",100);
foreach(DictionaryEntry Caches? in Cache)
{
sb.Append("key=").Append(Caches.Key.ToString()).Append("
") ;
sb.Append("value=").Append(Caches.Value.ToString()).Append("
");
}
Response.Write(sb.ToString());
查看Cache中的項(xiàng)數(shù)
int Count=Cache.Count;
Response.Write(Count.ToString());
將數(shù)據(jù)從緩存中刪除
Cache.Remove("txt");
使Cache具有文件依賴項(xiàng)或鍵依賴項(xiàng)的對(duì)象
我們?cè)谝豁撁娼?個(gè)按鈕,查看CACHE是否存在
在窗體啟動(dòng)時(shí)我們創(chuàng)建CACHE,名稱="txt2",數(shù)值=數(shù)據(jù)集ds
該CACHE與myfile.xml相關(guān)聯(lián),當(dāng)myfile.xml文件變化時(shí),txt2CACHE就被自動(dòng)刪除
private void Page_Load(object sender, System.EventArgs e)
??{
???if( !IsPostBack? )
???{
???string FilePath=MapPath("myfile.xml");
???SqlConnection con=new SqlConnection("Uid=sa;database=pubs;");
???SqlDataAdapter da =new SqlDataAdapter("select * from authors",con);
???DataSet ds=new DataSet();
???da.Fill(ds);
???System.Web.Caching.CacheDependency CacheDependencyXmlFile=new System.Web.Caching.CacheDependency(FilePath);
???Cache.Insert("txt2",ds ,CacheDependencyXmlFile);
???}
??}
為了監(jiān)視pubs數(shù)據(jù)庫authors表的變化
我們可以在pubs數(shù)據(jù)庫authors表建立觸發(fā)器
一旦authors表中發(fā)生增加,刪除,修改數(shù)據(jù)時(shí),觸發(fā)器會(huì)自動(dòng)修改文件myfile.xml
一旦myfile.xml文件變化,txt2CACHE就被系統(tǒng)自動(dòng)刪除
CREATE TRIGGER tr_authors
ON authors
FOR INSERT, UPDATE ,delete
AS
declare @cmd nvarchar(4000)
select @cmd='bcp "select convert(nvarchar(30),Getdate(),13)" queryout D:\cache\WebCache\myfile.xml -c -Sglc2403 -Usa -P'
exec master..xp_cmdshell @cmd
GO
private void QueryButton_Click(object sender, System.EventArgs e)
{
if ( Cache["txt2"]!=null)
{
?Response.Write("exists");
}
else
{
?Response.Write("not exists");
}
}
首先我們點(diǎn)按鈕,顯示Cache["txt2"]存在
現(xiàn)在我們?nèi)ケ韆uthors中任意修改一數(shù)據(jù),再點(diǎn)按鈕,顯示Cache["txt2"]不存在拉
以上我們是把CACHE是和一個(gè)文件相關(guān)聯(lián),我們還可以把CACHE和文件組關(guān)聯(lián),建立依賴
以下我們把CACHE和2個(gè)文件(myfile.xml,myfile1.xml)關(guān)聯(lián),一旦文件組中其中任意一文件變化,Cache會(huì)把"txt2"項(xiàng)的數(shù)據(jù)從CACHE中刪除
string[] FilePath=new String[]{MapPath("myfile.xml"),MapPath("myfile1.xml")};
System.Web.Caching.CacheDependency CacheDependencyXmlFile=new???????????????????? System.Web.Caching.CacheDependency(FilePath);
string CacheVaule="a";
Cache.Insert("txt2", CacheVaule ,CacheDependencyXmlFile);
?
緩存依賴可以是文件,還可以是其他對(duì)象的鍵
下面的代碼說明緩存Cache["txt2"]既依賴文件myfile.xml,又依賴緩存中的Cache["txt"],只要這2者任意一樣改變,緩存Cache["txt2"]就會(huì)清除
Cache["txt"] = "b";
string[] FilePath=new String[]{ MapPath("myfile.xml")};
string[] dependencyKey=new String[]{"txt"};
SqlConnection con=new SqlConnection("Uid=sa;database=pubs;");
SqlDataAdapter da =new SqlDataAdapter("select * from authors",con);
DataSet ds=new DataSet();
da.Fill(ds);
System.Web.Caching.CacheDependency CacheDependencyXmlFile=
????????? new System.Web.Caching.CacheDependency(FilePath,dependencyKey);
Cache.Insert("txt2",ds ,CacheDependencyXmlFile);
緩存絕對(duì)過期
緩存Cache["txt3"] 在1小時(shí)后自動(dòng)過期
DateTime absoluteExpiration =DateTime.Now.AddHours(1);
Cache.Insert("txt3","aa",null,absoluteExpiration,System.Web.Caching.Cache.NoSlidingExpiration);
緩存相對(duì)(滑動(dòng))過期
注意:如果創(chuàng)建的彈性到期時(shí)間小于零或大于一年,則將引發(fā)異常
緩存Cache["txt4"] 在最后一次被訪問后1小時(shí)自動(dòng)過期
TimeSpan slidingExpiration=TimeSpan.FromHours(1);
Cache.Insert("txt4","4",null,System.Web.Caching.Cache.NoAbsoluteExpiration,slidingExpiration);
緩存項(xiàng)的優(yōu)先等級(jí)
當(dāng)承載 ASP.NET 應(yīng)用程序的 Web 服務(wù)器缺少內(nèi)存時(shí),Cache 將有選擇地清除項(xiàng)來釋放系統(tǒng)內(nèi)存。當(dāng)向緩存添加項(xiàng)時(shí),可以為其分配與緩存中存儲(chǔ)的其他項(xiàng)相比較的相對(duì)優(yōu)先級(jí)。在服務(wù)器處理大量請(qǐng)求時(shí),分配了較高優(yōu)先級(jí)值的項(xiàng)被從緩存刪除的可能性較小,而分配了較低優(yōu)先級(jí)值的項(xiàng)則更有可能被刪除。
由CacheItemPriority 枚舉表示,默認(rèn)為 Normal。
緩存Cache["txt5"]優(yōu)先等級(jí)設(shè)為最高等級(jí),在服務(wù)器釋放系統(tǒng)內(nèi)存時(shí),該緩存項(xiàng)最不可能被刪除。
Cache.Insert("txt5","5",null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.High,null);
緩存項(xiàng)時(shí)通知應(yīng)用程序的回調(diào)方法
ASP.NET 提供 CacheItemRemovedCallback 委托。它定義編寫事件處理程序時(shí)使用的簽名,當(dāng)從緩存中刪除項(xiàng)時(shí),該事件處理程序?qū)⑦M(jìn)行響應(yīng)。
static System.Web.Caching.CacheItemRemovedReason reason;
System.Web.Caching.CacheItemRemovedCallback onRemove = null;
public void RemovedCallback(String k, Object v, System.Web.Caching.CacheItemRemovedReason r)
{
?itemRemoved = true;
?reason = r;
}
onRemove = new System.Web.Caching.CacheItemRemovedCallback (this.RemovedCallback);
Cache.Insert("txt",ds,null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,onRemove);
?
由于任何原因從Cache中移除時(shí),將調(diào)用 RemovedCallback 方法
轉(zhuǎn)載于:https://www.cnblogs.com/jiangchongwei/archive/2008/09/12/1289684.html
總結(jié)
以上是生活随笔為你收集整理的转载:缓存 Cache的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ListView控件学习系列2-编辑Li
- 下一篇: 关于php漏洞