简单的C#ASP.NET缓存实现 [英] Simple C# ASP.NET Cache Implementation
本文介绍了简单的C#ASP.NET缓存实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要建立一个列表与LT;对象>
和缓存清单,并能够添加到它。我还需要能够吹去容易并重新创建。什么是做到这一点的简单方法?
I need to build up a List<object>
and cache the list and be able to append to it. I also need to be able to blow it away easily and recreate it. What is a simple way to accomplish this?
推荐答案
这样的事情吧?
using System;
using System.Collections.Generic;
using System.Web;
public class MyListCache
{
private List<object> _MyList = null;
public List<object> MyList {
get {
if (_MyList == null) {
_MyList = (HttpContext.Current.Cache["MyList"] as List<object>);
if (_MyList == null) {
_MyList = new List<object>();
HttpContext.Current.Cache.Insert("MyList", _MyList);
}
}
return _MyList;
}
set {
HttpContext.Current.Cache.Insert("MyList", _MyList);
}
}
public void ClearList() {
HttpContext.Current.Cache.Remove("MyList");
}
}
至于如何使用.....
As for how to use.....
// Get an instance
var listCache = new MyListCache();
// Add something
listCache.MyList.Add(someObject);
// Enumerate
foreach(var o in listCache.MyList) {
Console.WriteLine(o.ToString());
}
// Blow it away
listCache.ClearList();
这篇关于简单的C#ASP.NET缓存实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文