【问题标题】:Console Application Memory Cache控制台应用程序内存缓存
【发布时间】:2015-11-07 11:17:29
【问题描述】:

我已经研究过,联系了朋友,但仍然无法让它发挥作用。即使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;
....
}

有些东西我显然不明白,非常感谢任何人对此的帮助!

【问题讨论】:

  • 您能更清楚地描述您面临的问题吗?

标签: c# caching memcached console-application


【解决方案1】:

好,我们来看几点:

  • 为什么要从MemoryCache 转换为ObjectCache?在这段代码中,我看不出有任何理由这样做;
  • 为什么要测试 3 种方法来获取缓存? GetCacheItem 将返回一个 CacheItem 对象,因此您的代码将始终在那里失败(在 as 演员表中)。查看您的代码,我认为仅使用 Get 方法就足够了(如果缓存不存在,它将返回 null);
  • 您的时间政策看起来不错。如果您的日程安排与策略到期时间完全相同,您将永远不会刷新缓存。

所以,我使用 15 秒过期策略对您的代码进行了测试,只是更改了您可以看到它有效的时间。请仔细查看您的计划时间并将其与您的代码进行比较。

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;
}

这是控制台结果:

查看缓存

不,不在缓存中

添加到缓存中...

它在测试缓存中!

等待 10 秒...

查看缓存

等待 10 秒...

查看缓存

不,不在缓存中

添加到缓存中...

它在测试缓存中!

按下一个键...

【讨论】:

  • 它像一吨砖一样击中了我。当应用程序作为计划任务启动时,缓存中始终没有任何内容。我需要应用程序循环连续运行 12 小时。第一次它必须获取数据,但接下来的 11 次缓存将在那里。回到绘图板。接受你的回答,因为它打开了灯。谢谢。
猜你喜欢
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多