相关疑难解决方法(0)

具有超时/过期能力的列表

我需要一个这样的函数:

AddToList(txtName, timeExpire);
Run Code Online (Sandbox Code Playgroud)

它看起来像是自描述的,该项目将自动过期并从列表中删除.

我无法想象如何做到这一点.任何人都可以给我一个线索吗?

c# list

19
推荐指数
3
解决办法
2万
查看次数

.Net Core MemoryCache PostEvictionCallback无法正常工作

我在Microsoft.Extensions.Caching.Memory.MemoryCache中设置了具有滑动过期的缓存项.我想在每次缓存项到期时触发回调,但在我查询缓存中过期的缓存项之前,不会触发回调.

这是代码:

using System;
using Microsoft.Extensions.Caching.Memory;

namespace Memcache
{
    public class Program
    {
        private static MemoryCache _cache;
        private static int _cacheExpSecs;

        public static void Main(string[] args)
        {
            _cache = new MemoryCache(new MemoryCacheOptions());
            _cacheExpSecs = 2;

            var cacheEntryOptions = new MemoryCacheEntryOptions()
            .SetSlidingExpiration(TimeSpan.FromSeconds(_cacheExpSecs))
            .RegisterPostEvictionCallback(callback: EvictionCallback);

            _cache.Set(1, "One", cacheEntryOptions);
            _cache.Set(2, "Two", cacheEntryOptions);

            var autoEvent = new System.Threading.AutoResetEvent(false);

            System.Threading.Timer timer = new System.Threading.Timer(checkCache, autoEvent, 1000, 6000);

            Console.Read();
        }

        private static void checkCache(Object o)
        {
            if(_cache.Get(1)!=null)
            {
                Console.WriteLine(string.Format(@"checkCache: Cache with key {0} will be removed …
Run Code Online (Sandbox Code Playgroud)

.net caching memorycache asp.net-core

7
推荐指数
2
解决办法
3897
查看次数

标签 统计

.net ×1

asp.net-core ×1

c# ×1

caching ×1

list ×1

memorycache ×1