【问题标题】:Data caching .net expires before time specified数据缓存 .net 在指定时间之前过期
【发布时间】:2013-06-07 20:51:25
【问题描述】:

我的应用程序中有一个数据缓存,它保存查询结果并在每个“对小时”即(08:00、10:00、12:00)绝对过期。

所以:

 public Dictionary<int, List<Int64>> GetCacheDNAOffers()
        {
            if (_cache["DnaOffersPairs"] == null)
            {
                Dictionary<int, string> dtDNA = GetCacheActiveOffers().AsEnumerable().Select(d => new { idOferta = Convert.ToInt32(d["nof_id"]), dna = d["nof_dna"].ToString() }).ToDictionary(t => t.idOferta, t => t.dna);
                Dictionary<int, List<Int64>> lstDNA = new Dictionary<int, List<Int64>>();

                foreach (var dna in dtDNA)
                {
                    lstDNA[dna.Key] = CarregarDNA(dna.Value);
                }
                _cache.Insert("DnaOffersPairs", lstDNA, null, SetTimeCacheExpires(), TimeSpan.Zero);
            }

            return (Dictionary<int, List<Int64>>)_cache["DnaOffersPairs"];
        }

设置过期时间的功能:

private DateTime SetTimeCacheExpires()
{
        try
        {

 int hour = DateTime.Now.Hour;
            int minutes = DateTime.Now.Minute;
            int qtdhoursToExpire = 1;

            NextHours = (hour + qtdhoursToExpire);

        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, PairHour, minutes, 1);

    }
    catch (Exception ex)
    {
        throw ex;
    }
}

以及检查“对小时”的功能:

 public int PairHour
    {
        get
        {
            if (NextHours % 2 == 0)
                return NextHours;
            else
                return NextHours + 1;
        }
    }

有时会出现缓存过期的问题,如果应用程序空闲一段时间,所有浏览器窗口都关闭。

是否有我需要在 IIS 上设置的配置来设置该缓存永久保持活动状态直到预配置缓存结束?还是代码上的缓存配置设置正确?

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    您应该检查这是否是由 IIS 池回收引起的。默认情况下,应用程序池会在空闲 20 分钟后回收。当回收发生时,存储在缓存中的所有数据都将过期(该过程只是简单地关闭)。 http://technet.microsoft.com/pl-pl/library/cc753179(v=ws.10).aspx

    【讨论】:

    • 29 小时(1740 分钟),而不是 20 分钟
    【解决方案2】:

    已解决:

    IIS 上有一个默认配置,如果应用程序空闲 20 分钟,它会确定缓存超时。

    你必须删除它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 2012-09-20
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      相关资源
      最近更新 更多