Caching in C# without System.Web

2019-04-08 14:47发布

I want to be able to cache a few objects without referencing System.Web. I want sliding expiration and little more... Is there really no where to go but to build my own using a Dictionary and some selfmade expiration of objects - or are there something in the .NET framework I've totally missed out on?

标签: c# caching
2条回答
\"骚年 ilove
2楼-- · 2019-04-08 15:09

You could try the Microsoft Enterprise Library Caching Application Block.

Example utility function for caching on demand with a sliding timeout:

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;
    }
}

You can specify multiple expiration policies, including SlidingTime, FileDependency, ExtendedFormatTime, or you can write your own.

查看更多
干净又极端
3楼-- · 2019-04-08 15:18
登录 后发表回答