开发者

About make variable expires after centain time (cachced data)?

I have a data object that will store the data that request from database, these data is cached for store only 5 min, after that 5 min it will request from database again.

So something like below:

public class UserCachedData
{
    List<string> _SelectableProviderList { get; set; }

    DateTime _Selectabl开发者_运维技巧eProviderList_RequestDateTime = DateTime.MinValue;
    public List<string> SelectableProviderList {
        get {
            if (_SelectableProviderList == null || _SelectableProviderList_RequestDateTime < DateTime.Now)
            {
                _SelectableProviderList_RequestDateTime = DateTime.Now.AddMinutes(5);
                _SelectableProviderList = QueryService.GetList();
            }
            return _SelectableProviderList; 
        }
        set { 
            _SelectableProviderList = value; 
        }
    }
}

Here comes the question, I am going to have a lot of these variable in this class, all of them will have a expire time, I wonder what's the best way to avoid writting a Expire time for each of them? Is there any generic way I can have each variable in this class will expires after centain time and do centain action (like call QueryService to get new item list this time)?

Thanks in advance,

King


Just use the Cache class. That's what it's for.

Though it lives in the System.Web.Caching namespace, it is a normal .NET class and can be used in non web applications.


Update:

If you are using .NET 4.0 you should use MemoryCache for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜