首页 > 解决方案 > 找不到asp.net缓存键

问题描述

我正在运行 asp.net 应用程序。在一个函数中,我尝试添加一个缓存以供以后使用。但是,日志总是说找不到缓存键。似乎没有添加缓存键。首次加载页面时,提示“找不到”,这是正常的。然后我重新加载页面,它应该找到缓存键,但它仍然说找不到缓存键。我的代码有什么问题?这是我的代码

using System.Runtime.Caching;
public class ReportManager
{
    protected static MemoryCache CACHE = new MemoryCache("Report_Cache");

    public static DataSet KPISummaryReport(int companyID, int fromYear, int fromMonth, int toYear, int toMonth, string locationIDs, bool hideIfNoValue, string lang)
    {
        HttpResponseMessage result = null;
        string requestUri = string.Format("Report/KPISummaryReport/?companyID={0}&fromYear={1}&fromMonth={2}&toYear={3}&toMonth={4}&locationIDs={5}&hideIfNoValue={6}&fmt=xml&lang={7}"
            , companyID, fromYear, fromMonth, toYear, toMonth, locationIDs, hideIfNoValue, lang);
        DataSet ds = null; 

        string cacheKey = "kpi_" + companyID + "_" + fromYear + "_" + fromMonth + "_" + toYear + "_" + toMonth + "_" + locationIDs;

        ds = CACHE.Get(cacheKey) as DataSet;

        if (ds != null)
        {
            Logger.Log(string.Format("Found the report dataset cache key {0}",cacheKey));
            return ds;                    
        }
        else
        {
            Logger.Log(string.Format("Cannot find the report dataset cache key {0}",cacheKey));
            ds = Util.GetData(_sustainabilityServiceURL, requestUri, out result);
            var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(60d) };
            CACHE.Add(cacheKey, ds, policy);
        }

        return ds;
    }
}

标签: c#asp.netcaching

解决方案


推荐阅读