List with timeout/expire ability

后端 未结 4 471
走了就别回头了
走了就别回头了 2020-12-16 12:11

I need a function like this:

AddToList(txtName, timeExpire);

It looks like self-descriptive, the item will automatically expire and removed

4条回答
  •  佛祖请我去吃肉
    2020-12-16 12:42

    Just a quick example for the answer Will gave

    Add reference to System.Runtime.Caching

     MemoryCache cache = new MemoryCache("CacheName");
    
     cache.Add(key, value, new CacheItemPolicy() 
                               { AbsoluteExpiration = DateTime.UtcNow.AddSeconds(20) });
    

    You can then check if something is still in the cache by going

     if (cache.Contains(key))
    

    And if you want your cache to have a low time limit then you will need to change the refresh time of the cache with a nice little hack.

    MemoryCache AbsoluteExpiration acting strange

提交回复
热议问题