示例#1
0
        public void Test_cache_on_selected_method_with_custom_cache_store_type()
        {
            var cacheStore = Substitute.For <ICacheStore>();
            var builder    = new ContainerBuilder();

            builder.RegisterModule(new FlatwhiteCoreModule());
            builder
            .RegisterType <BlogService>()
            .As <IBlogService>()
            .SingleInstance()
            .CacheWithStrategy(
                CacheStrategies.ForService <IBlogService>()
                .ForMember(x => x.GetById(Argument.Any <Guid>()))
                .Duration(5)
                .WithCacheStoreType(cacheStore.GetType())
                );

            builder.RegisterInstance(cacheStore).As <ICacheStore>();
            var container = builder.Build();

            var cachedService      = container.Resolve <IBlogService>();
            var cacheStoreProvider = Substitute.For <ICacheStoreProvider>();

            cacheStoreProvider.GetCacheStore(Arg.Is <Type>(t => t == cacheStore.GetType())).Returns(cacheStore);
            Global.CacheStoreProvider = cacheStoreProvider;
            var id = Guid.NewGuid();

            for (var i = 0; i < 10; i++)
            {
                var result = cachedService.GetById(id);
            }

            cacheStore.Received().Get(Arg.Any <string>());
        }
示例#2
0
        public async Task Test_cache_on_selected_method()
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule(new FlatwhiteCoreModule());
            builder
            .RegisterType <BlogService>()
            .As <INoneCaheBlogService>()
            .CacheWithStrategy(
                CacheStrategies.ForService <INoneCaheBlogService>()
                .ForMember(x => x.GetByIdAsync(Argument.Any <Guid>()))
                .Duration(1000)
                .VaryByCustom("custom")
                .VaryByParam("postId")
                );

            var container = builder.Build();

            var cachedService = container.Resolve <INoneCaheBlogService>();

            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();

            for (var i = 0; i < 10; i++)
            {
                var b1 = await cachedService.GetByIdAsync(id1);

                var b2 = await cachedService.GetByIdAsync(id2);
            }

            dynamic blogSvc = cachedService;

            Assert.AreEqual(2, blogSvc.__target.InvokeCount);
        }
        public async Task Should_not_throw_exception_when_cannot_resolve_KeyService_by_id()
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule(new FlatwhiteCoreModule());

            builder
            .RegisterType <BlogService>()
            .As <INoneCaheBlogService>()
            .CacheWithStrategy(
                CacheStrategies.ForService <INoneCaheBlogService>()
                .ForMember(x => x.GetByIdAsync(Argument.Any <Guid>()))
                .Duration(1000)
                .VaryByCustom("custom")
                .VaryByParam("postId")
                );

            var container = builder.Build();

            KeyInterceptorRegistrationSource.DynamicAttributeCache.Clear();
            var cachedService = container.Resolve <INoneCaheBlogService>();


            var id = Guid.NewGuid();

            for (var i = 0; i < 10; i++)
            {
                var name1 = await cachedService.GetByIdAsync(id);
            }
            dynamic blogSvc = cachedService;

            Assert.AreEqual(10, blogSvc.__target.InvokeCount);
        }
示例#4
0
        public void Test_cache_on_selected_method_with_change_monitor()
        {
            var blogService = Substitute.For <IBlogService>();
            var wait        = new CountdownEvent(2);

            blogService.When(x => x.GetById(Arg.Any <Guid>())).Do(c =>
            {
                c.Returns(new object {});
                wait.Signal();
            });

            FlatwhiteCacheEntryChangeMonitor mon = null;
            var builder = new ContainerBuilder();

            builder.RegisterModule(new FlatwhiteCoreModule());
            builder
            .RegisterInstance(blogService)
            .As <IBlogService>()
            .SingleInstance()
            .CacheWithStrategy(
                CacheStrategies.ForService <IBlogService>()
                .ForMember(x => x.GetById(Argument.Any <Guid>()))
                .Duration(100000)
                .StaleWhileRevalidate(500)
                .VaryByParam("postId")
                .WithCacheStore(0)
                .WithRevalidateKeyFormat("posts")
                .WithChangeMonitors((i, context) =>
            {
                mon = new FlatwhiteCacheEntryChangeMonitor("revalidationKey");
                return(new[] { mon });
            })
                );

            var container = builder.Build();

            var cachedService = container.Resolve <IBlogService>();

            var id = Guid.NewGuid();

            for (var i = 0; i < 1000; i++)
            {
                var result = cachedService.GetById(id);
            }
            //NOTE: After this, the cache item should be refreshed by Phoenix
            mon.OnChanged(null);

            for (var i = 0; i < 1000; i++)
            {
                var result = cachedService.GetById(id);
            }
            //NOTE: Because the phoenix reborn is none-blocking (on a background thread), it may need time to let the Task finish.
            Assert.IsTrue(wait.Wait(2000));
            mon.Dispose();
        }