我需要建立一个 List<object>
并缓存列表并能够附加到它。我还需要能够轻松地将它吹走并重新创建它。实现此目的的简单方法是什么?
最佳答案
也许是这样的?
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");
}
}
至于如何使用......
// 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# - 简单的 C# ASP.NET 缓存实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2458205/