我有一个 asp.net 函数,用作报告的数据源。当我第一次运行该函数时,我缓存了数据集,发现缓存创建成功,因为缓存计数为1。但是,当我再次进入该函数时,我无法获取缓存内容。并且缓存计数为零。似乎由于某种原因缓存被清除了。如何找出重新进入页面时缓存计数为零的原因以及如何使缓存发挥作用?这是我的代码:
//using System.Web.Caching;
using System.Runtime.Caching;
namespace Sustainability.BusinessObject.Client
{
public class ReportManager
{
protected static MemoryCache CACHE = new MemoryCache("MySQLDataProvider_Cache");
public static DataSet KPISummaryReport(int companyID, int fromYear, int fromMonth, int toYear, int toMonth, string locationIDs, bool hideIfNoValue, string lang)
{
HttpResponseMessage result = null;
DataSet ds = null;
try
{
string cacheKey = "kpi_" + companyID + "_" + fromYear + "_" + fromMonth + "_" + toYear + "_" + toMonth + "_" + locationIDs;
Logger.Log(string.Format("Cache count of reentering the code {0}", CACHE.GetCount()));
ds = CACHE.Get(cacheKey) as DataSet;
if (ds != null)
{
return ds;
}
else
{
ds = Util.GetData(_sustainabilityServiceURL, requestUri, out result);
var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(60d) };
CACHE.Add(cacheKey, ds, policy);
DataSet ds1 = CACHE.Get(cacheKey) as DataSet;
if (ds1 != null)
{
Logger.Log("Create Cache Succesfully");
}
else
{
Logger.Log("Create Cache Failed");
}
Logger.Log(string.Format("Cache count after create cache {0}",CACHE.GetCount()));
}
}
}
哈士奇WWW
相关分类