c# - 简单的 C# ASP.NET 缓存实现

标签 c# asp.net caching

我需要建立一个 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/

相关文章:

c# - 在没有头文件的情况下声明 COM 接口(interface)指针

asp.net - 我们可以在单个元素上使用多个 itemprop 来进行微数据标记吗

c# - 动态生成的控件 ID 返回为 NULL

php - include() 是否使用 file_exists() 的缓存?

ios - 如何用很多图像加快 tableview 的性能?

c# - 在另一个表格中设置表格宽度未反射(reflect)在创建的 pdf 中

c# - 错误处理 MVC View 与 PartialView

c# - 检查 SELECT 语句条件是否为真

asp.net - 在 Azure 中实现全文搜索有哪些可能的解决方案?

php - 检查图像修改时间的 HTACCESS 图像缓存规则