找不到asp.net缓存键

时间:2019-07-26 07:35:16

标签: c# asp.net caching

我正在运行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;
    }
}

0 个答案:

没有答案