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>()); }
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 void Test_cache_all_method_strategy_on_registered_interface_type_service() { var builder = new ContainerBuilder(); builder.RegisterModule(new FlatwhiteCoreModule()); builder .RegisterType <BlogService>() .As <IBlogService>() .CacheWithStrategy(CacheStrategies.AllMethods().VaryByParam("postId")); var container = builder.Build(); var cachedService = container.Resolve <IBlogService>(); var id1 = Guid.NewGuid(); var id2 = Guid.NewGuid(); for (var i = 0; i < 10; i++) { var b1 = cachedService.GetById(id1); var b2 = cachedService.GetById(id2); } dynamic blogSvc = cachedService; Assert.AreEqual(2, blogSvc.__target.InvokeCount); }
public void Test_cache_all_method_strategy_on_registered_intance_service() { var id1 = Guid.Parse("3dd19adf-b743-43d9-add4-19f85dc857da"); var id2 = Guid.Parse("d5be1152-92b8-4a7f-a837-5365d7c73001"); var svc = Substitute.For <IUserService>(); svc.GetById(id1).Returns(new { Name = "Van", Email = "*****@*****.**", Id = id1 }); svc.GetById(id2).Returns(new { Name = "Billy", Email = "*****@*****.**", Id = id2 }); var builder = new ContainerBuilder(); builder.RegisterModule(new FlatwhiteCoreModule()); builder .RegisterInstance(svc) .As <IUserService>() .CacheWithStrategy(CacheStrategies.AllMethods().VaryByParam("userId").VaryByCustom("custom").Duration(10)); var container = builder.Build(); var cachedService = container.Resolve <IUserService>(); for (var i = 0; i < 10; i++) { var name1 = cachedService.GetById(id1); var name2 = cachedService.GetById(id2); } svc.Received(1).GetById(Arg.Is(id1)); svc.Received(1).GetById(Arg.Is(id2)); }
public void Test_cache_all_method_strategy_should_obay_nocache_attribute() { var svc = Substitute.For <IUserService>(); string email = "*****@*****.**"; svc.GetByEmail(email).Returns(new { Name = "Van", Email = email, Id = "1" }); var builder = new ContainerBuilder(); builder.RegisterModule(new FlatwhiteCoreModule()); builder .RegisterInstance(svc) .As <IUserService>() .CacheWithStrategy(CacheStrategies.AllMethods().VaryByParam("email")); var container = builder.Build(); var cachedService = container.Resolve <IUserService>(); for (var i = 0; i < 10; i++) { dynamic user = cachedService.GetByEmail(email); Assert.AreEqual("Van", user.Name); } svc.Received(10).GetByEmail(Arg.Is(email)); }
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); }
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(); }
public void Test_cache_all_method_strategy_on_registered_class_type_service() { var builder = new ContainerBuilder().EnableFlatwhite(); builder .RegisterType <BlogService>() .CacheWithStrategy(CacheStrategies.AllMethods().VaryByParam("postId")); var container = builder.Build(); var cachedService = container.Resolve <BlogService>(); var id1 = Guid.NewGuid(); var id2 = Guid.NewGuid(); for (var i = 0; i < 10; i++) { var b1 = cachedService.GetById(id1); var b2 = cachedService.GetById(id2); } Assert.AreEqual(2, cachedService.InvokeCount); }