C#——Cache的理解及使用

(1)Cache有以下几条缓存数据的规则。

  1)数据可能会被频繁的被使用,这种数据可以缓存。
  2)数据的访问频率非常高,或者一个数据的访问频率不高,但是它的生存周期很长,这样的数据最好也缓存起来。

(2)Cache用法

1)将数据添加到缓存中
a.通过指定其键和值将项添加到缓存中
	Cache["txt"] = "a";
b.通过使用 Insert(重载Insert方法)方法将项添加到缓存中
	Cache.Insert("txt", "a");
2)从缓存中取得数据

方式1:

	string str=(string)Cache.Get("txt");

方式2:

	string str1=(string)Cache["txt1"];
3)查看Cache中所有数据
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(" ");
}
4)查看Cache中的项数
	int Count=Cache.Count;
5)将数据从缓存中删除
	Cache.Remove("txt");

 
参考:
C#中Cache的使用
c#中的Cache缓存技术

发布了68 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_35077107/article/details/104276570