国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C#中Cache的使用

2019-11-17 04:06:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
公共方法
Add 將指定項(xiàng)添加到 Cache 對(duì)象,該對(duì)象具有依賴項(xiàng)、過(guò)期和優(yōu)先級(jí)策略以及一個(gè)委托(可用于在從 Cache 移除插入項(xiàng)時(shí)通知應(yīng)用程序)。
Equals(從 Object 繼承) 已重載。確定兩個(gè) Object 實(shí)例是否相等。
Get 從 Cache 對(duì)象檢索指定項(xiàng)。
GetEnumerator 檢索用于循環(huán)訪問(wèn)包含在緩存中的鍵設(shè)置及其值的字典枚舉數(shù)。
GetHashCode(從 Object 繼承) 用作特定類型的哈希函數(shù),適合在哈希算法和數(shù)據(jù)結(jié)構(gòu)(如哈希表)中使用。
GetType(從 Object 繼承) 獲取當(dāng)前實(shí)例的 Type。
Insert 已重載。向 Cache 對(duì)象插入項(xiàng)。使用此方法的某一版本改寫(xiě)具有相同 key 參數(shù)的現(xiàn)有 Cache 項(xiàng)。
Remove 從應(yīng)用程序的 Cache 對(duì)象移除指定項(xiàng)。
ToString(從 Object 繼承) 返回表示當(dāng)前 Object 的 String。


public object Add(
   string key,
   object value,
   CacheDependency dependencies,
   DateTime absoluteExpiration,
   TimeSpan slidingExpiration,
   CacheItemPRiority priority,
   CacheItemRemovedCallback onRemoveCallback
);

參數(shù)
key
用于引用該項(xiàng)的緩存鍵。
value
要添加到緩存的項(xiàng)。
dependencies
該項(xiàng)的文件依賴項(xiàng)或緩存鍵依賴項(xiàng)。當(dāng)任何依賴項(xiàng)更改時(shí),該對(duì)象即無(wú)效,并從緩存中移除。如果沒(méi)有依賴項(xiàng),則此參數(shù)包含空引用(Visual Basic 中為 Nothing)。
absoluteExpiration
所添加對(duì)象將過(guò)期并被從緩存中移除的時(shí)間。
slidingExpiration
最后一次訪問(wèn)所添加對(duì)象時(shí)和該對(duì)象過(guò)期時(shí)之間的時(shí)間間隔。如果該值等效于 20 分鐘,則對(duì)象在最后一次被訪問(wèn) 20 分鐘之后將過(guò)期并從緩存中移除。
priority
對(duì)象的相對(duì)成本,由 CacheItemPriority 枚舉表示。緩存在退出對(duì)象時(shí)使用該值;具有較低成本的對(duì)象在具有較高成本的對(duì)象之前被從緩存移除。
onRemoveCallback
在從緩存中移除對(duì)象時(shí)所調(diào)用的委托(如果提供)。當(dāng)從緩存中刪除應(yīng)用程序的對(duì)象時(shí),可使用它來(lái)通知應(yīng)用程序。
示例
public void AddItemToCache(Object sender, EventArgs e) {
    itemRemoved = false;

    onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

    if (Cache["Key1"] == null)
      Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, onRemove);
}

將數(shù)據(jù)添加到緩存中

1。通過(guò)指定其鍵和值將項(xiàng)添加到緩存中
Cache["txt"] = "a";

2.通過(guò)使用 Insert(重載Insert方法)方法將項(xiàng)添加到緩存中

Cache.Insert("txt", "a");

下列代碼顯示如何設(shè)置相對(duì)過(guò)期策略。它插入一個(gè)項(xiàng),該項(xiàng)自上次訪問(wèn)后 10 分鐘過(guò)期。注意 DateTime.MaxValue 的使用,它表示此項(xiàng)沒(méi)有絕對(duì)過(guò)期策略。

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 方法使用上沒(méi)有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è)谝豁?yè)面建立1個(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ù)庫(kù)authors表的變化
我們可以在pubs數(shù)據(jù)庫(kù)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ì)象的鍵
下面的代碼說(shuō)明緩存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ì)過(guò)期

緩存Cache["txt3"] 在1小時(shí)后自動(dòng)過(guò)期
DateTime absoluteExpiration =DateTime.Now.AddHours(1);
Cache.Insert("txt3","aa",null,absoluteExpiration,System.Web.Caching.Cache.NoSlidingExpiration);

緩存相對(duì)(滑動(dòng))過(guò)期

注意:如果創(chuàng)建的彈性到期時(shí)間小于零或大于一年,則將引發(fā)異常
緩存Cache["txt4"] 在最后一次被訪問(wèn)后1小時(shí)自動(dòng)過(guò)期
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)來(lái)釋放系統(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 委托。它定義編寫(xiě)事件處理程序時(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 方法
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 楚雄市| 巴青县| 锡林浩特市| 涞水县| 永川市| 梓潼县| 绍兴县| 弥勒县| 三河市| 五华县| 县级市| 永清县| 江孜县| 普安县| 田东县| 贵定县| 邹平县| 柏乡县| 特克斯县| 西贡区| 县级市| 奉化市| 神木县| 九龙坡区| 长子县| 武夷山市| 天气| 大洼县| 清丰县| 定结县| 化州市| 邵东县| 富民县| 阜康市| 深泽县| 紫阳县| 固安县| 辽中县| 丁青县| 高尔夫| 循化|