动态缓存不缓存数据 [英] Dynacache doesn't cache data

查看:52
本文介绍了动态缓存不缓存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Dynacache 来缓存最终将由服务调用返回的数据-以获取所需信息工作,我只是存根返回的数据.我正在使用DI的SimpleInjector并向Dynacache进行了注册,如我对上一个问题的答案所指出

I am using Dynacache to cache data that will be eventually returned from a service call - to get things working I am just stubbing the data returned. I am using SimpleInjector for DI and got Dynacache registered with it as pointed out in the answer I got to a previous question

所以我的解决方案中有一个集成项目,其中包含我要缓存的方法-当前如下所示:

So I have an integration project within my solution which contains the method I want to cache - which currently looks as below:

[CacheableMethod(30)]
public virtual List<MyResponse> GetDataByAccountNumber(int accountNumber)
{
    var response = StubResposne();

    return response;
}

通过上述实现,Dynacache应该将数据缓存30秒,然后将其清除.但是,如果我第一次在我的私有StubResponse()方法的第一行上设置一个断点,则当我第一次访问使用该数据的网页时,该断点将按预期命中,并返回数据.但是,如果我再次立即刷新页面,我期望的是,由于数据将被缓存(在30秒内),那么断点将不会被命中,但每次都会被命中?

With implementation above Dynacache should cache the data for 30 secs and then clear it. However if I set a breakpoint on the first line in my private StubResponse() method the first time I hit the webpage which uses that data the breakpoint gets hit as expected and the data is returned. However if I refresh the page straight away again I was expecting that as the data would have been cached (as its within 30 secs) then the breakpoint would not have been hit but it is hit everytime?

我使用Dynacache的方式有问题吗?

Is there something incorrect with how I am using Dynacache?

推荐答案

要向SimpleInjector注册,请按以下步骤操作:

For registering with SimpleInjector it should be done as below:

container.RegisterSingle<IDynaCacheService>(new MemoryCacheService());
container.Register(typeof(ITestClass), Cacheable.CreateType<TestClass>());

完成此操作后,缓存将按预期工作.

Once I done this Caching worked as expected.

已更新为完整的测试示例程序

Updated with Full test sample Program

using System;

namespace DynaCacheSimpleInjector

{
    using System.Threading;


using DynaCache;

using SimpleInjector;

class Program
{
    static void Main()
    {
        var container = new Container();

        container.RegisterSingle<IDynaCacheService>(new MemoryCacheService());
        container.Register(typeof(ITestClass), Cacheable.CreateType<TestClass>());

        var instance = container.GetInstance<ITestClass>();

        for (var i = 0; i < 10; i++)
        {
            // Every 2 seconds the output will change
            Console.WriteLine(instance.GetData(53));
            Thread.Sleep(500);
        }
    }
}

public class TestClass : ITestClass
{
    [CacheableMethod(2)]
    public virtual string GetData(int id)
    {
        return String.Format("{0} - produced at {1}", id, DateTime.Now);
    }
}

public interface ITestClass
{
    string GetData(int id);
}

}

这篇关于动态缓存不缓存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆