缓存在C#中没有的System.Web [英] Caching in C# without System.Web

查看:154
本文介绍了缓存在C#中没有的System.Web的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够缓存几个对象,而不引用System.Web程序。我想滑动过期多一点...难道真的没有地方可去,但使用字典和对象的一些自制到期建立自己 - ?还是有什么东西在NET框架我已经完全错过了

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?

推荐答案

您可以尝试的微软企业库缓存应用程序块

示例与滑动超时按需缓存实用功能:

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

您可以指定多个过期策略,包括 SlidingTime FileDependency ExtendedFormatTime ,也可以写自己的。

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

这篇关于缓存在C#中没有的System.Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆