我研究过,挖过朋友,仍然无法让这个工作。即使Caching in a console application也没有回答这个问题。
我有一个计划任务,在接下来的12个小时内每小时运行一次这个应用程序,使用去年的数据作为参考。
当然,去年的数据不会改变,这是使用缓存的完美案例,特别是因为查询需要90到110秒才能运行,具体取决于服务器的繁忙程度。
using System.Runtime.Caching;
private static ObjectCache o_Cache = MemoryCache.Default;
private static IEnumerable<SWFSalesModel> GetLastYearsSalesData(ObjectCache o_Cache, string cacheKey) {
IEnumerable<SWFSalesModel> arrLastYearsData = o_Cache.GetCacheItem(cacheKey) as IEnumerable<SWFSalesModel>;
if (arrLastYearsData == null) {
Console.WriteLine("Look in the cache 2");
arrLastYearsData = (IEnumerable<SWFSalesModel>)o_Cache.Get(cacheKey);
}
if (arrLastYearsData == null) {
Console.WriteLine("Look in the cache 3");
arrLastYearsData = (IEnumerable<SWFSalesModel>)o_Cache[cacheKey];
}
if (arrLastYearsData != null) {
return arrLastYearsData;
} else {
Console.WriteLine("nope, not in the cache");
arrLastYearsData = GetLastYearSales(); ;
CacheItemPolicy policy = new CacheItemPolicy();
Console.WriteLine("Adding to the cache...");
policy.AbsoluteExpiration = DateTimeOffset.Now.AddHours(12.0);
o_Cache.Add(cacheKey, arrLastYearsData, policy);
IEnumerable<SWFSalesModel> arrTest = (IEnumerable<SWFSalesModel>)o_Cache[cacheKey];
if (arrTest != null) {
/* This works 100% of the time */
Console.WriteLine("it's in test cache!");
} else {
Console.WriteLine("nope, nada in test cache");
}
return arrLastYearsData;
}
}
然后在程序中调用:
/* Get Last Years Sales Data */
string cacheKey = DateTime.Today.ToString("MM/dd/yyyy");
IEnumerable<SWFSalesModel> arrLastYearSales = GetLastYearsSalesData(o_Cache, cacheKey);
我也尝试过在函数中引用内存缓存:
private static IEnumerable<SWFSalesModel> GetLastYearsSalesData(string cacheKey) {
ObjectCache o_Cache = MemoryCache.Default;
....
}
有些东西我显然不明白,并会非常感谢任何人的帮助!
答案 0 :(得分:0)
好的,让我们看一些观点:
as
强制转换)。查看一段代码,我认为只使用Get方法就足够了(在缓存不存在的情况下它会返回null); 因此,我使用15s过期策略对您的代码进行了测试,只是更改了您可以看到它的工作时间。请仔细查看您的日程安排时间,并将其与您的代码进行比较。
private static MemoryCache o_Cache = MemoryCache.Default;
static void Main(string[] args)
{
string cacheKey = DateTime.Today.ToString("MM/dd/yyyy");
IEnumerable<SWFSalesModel> arrLastYearSales = GetLastYearsSalesData(o_Cache, cacheKey);
Console.WriteLine();
Console.WriteLine("Waiting 10 seconds...");
Console.WriteLine();
System.Threading.Thread.Sleep(10000);
arrLastYearSales = GetLastYearsSalesData(o_Cache, cacheKey);
Console.WriteLine();
Console.WriteLine("Waiting 10 seconds...");
Console.WriteLine();
System.Threading.Thread.Sleep(10000);
arrLastYearSales = GetLastYearsSalesData(o_Cache, cacheKey);
Console.WriteLine();
Console.WriteLine("Press a key...");
Console.ReadKey();
}
private static IEnumerable<SWFSalesModel> GetLastYearsSalesData(MemoryCache o_Cache, string cacheKey)
{
Console.WriteLine("Looking at the cache");
var arrLastYearsData = (IEnumerable<SWFSalesModel>)o_Cache.Get(cacheKey);
if (arrLastYearsData != null)
{
return arrLastYearsData;
}
else
{
Console.WriteLine("nope, not in the cache");
arrLastYearsData = GetLastYearSales(); ;
CacheItemPolicy policy = new CacheItemPolicy();
Console.WriteLine("Adding to the cache...");
policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(15);
o_Cache.Add(cacheKey, arrLastYearsData, policy);
IEnumerable<SWFSalesModel> arrTest = (IEnumerable<SWFSalesModel>)o_Cache[cacheKey];
if (arrTest != null)
{
Console.WriteLine("it's in test cache!");
}
else
{
Console.WriteLine("nope, nada in test cache");
}
return arrLastYearsData;
}
}
// just a test
private static IEnumerable<SWFSalesModel> GetLastYearSales()
{
List<SWFSalesModel> result = new List<SWFSalesModel>();
result.Add(new SWFSalesModel());
result.Add(new SWFSalesModel());
result.Add(new SWFSalesModel());
return result;
}
这是控制台结果:
查看缓存
nope,不在缓存中
添加到缓存...
它在测试缓存中!
等待10秒......
查看缓存
等待10秒......
查看缓存
nope,不在缓存中
添加到缓存...
它在测试缓存中!
按键......