在没有System.Web的C#中缓存

时间:2009-09-29 11:19:18

标签: c# caching

我希望能够在不引用System.Web的情况下缓存一些对象。我想要滑动过期而不是更多...真的没有去哪里但是使用字典和一些自制的对象过期来构建我自己的东西 - 或者.NET框架中有什么东西我完全错过了吗? / p>

2 个答案:

答案 0 :(得分:5)

您可以尝试Microsoft Enterprise Library Caching Application Block

使用滑动超时按需缓存的示例实用程序函数:

static T GetCached<T>(string key, TimeSpan timeout, Func<T> getDirect) {
    var cache = CacheFactory.GetCacheManager();
    object valueCached = cache[key];
    if(valueCached != null) {
        return (T) valueCached;
    } else {
        T valueDirect = getDirect();
        cache.Add(key, valueDirect, CacheItemPriority.Normal, null, new SlidingTime(timeout));
        return valueDirect;
    }
}

您可以指定多个过期策略,包括SlidingTimeFileDependencyExtendedFormatTime,或者您可以编写自己的过期策略。

答案 1 :(得分:2)

相关问题