async Task concurrent_providers()
        {
            const int iterations = 100000;

            var tasks = Enumerable.Range(0, iterations).Select(i =>
            {
                return(Task.Run(() =>
                {
                    var options = new RedisTempDataProviderOptions()
                    {
                        UserProvider = new FakeTempDataUserProvider("user" + i + "@example.com"),
                        Serializer = new NetDataContractTempDataSerializer()
                    };
                    var tdp = new RedisTempDataProvider(options, GetDatabase());
                    var context = new ControllerContext();
                    var tempData = tdp.LoadTempData(context);
                    tempData["id"] = 123;
                    tempData["user"] = new FakeItem()
                    {
                        Name = "User" + i
                    };
                    tdp.SaveTempData(context, tempData);

                    tempData = tdp.LoadTempData(context);
                    tempData["id"].ShouldEqual(123);
                    tempData["user"].ShouldEqual(new FakeItem()
                    {
                        Name = "User" + i
                    });
                }));
            });

            await Task.WhenAll(tasks);
        }
        async Task concurrent_providers()
        {
            const int iterations = 100000;

            var tasks = Enumerable.Range(0, iterations).Select(i =>
            {
                return Task.Run(() =>
                {
                    var options = new RedisTempDataProviderOptions()
                    {
                        UserProvider = new FakeTempDataUserProvider("user" + i + "@example.com"),
                        Serializer = new NetDataContractTempDataSerializer()
                    };
                    var tdp = new RedisTempDataProvider(options, GetDatabase());
                    var context = new ControllerContext();
                    var tempData = tdp.LoadTempData(context);
                    tempData["id"] = 123;
                    tempData["user"] = new FakeItem() { Name = "User" + i };
                    tdp.SaveTempData(context, tempData);

                    tempData = tdp.LoadTempData(context);
                    tempData["id"].ShouldEqual(123);
                    tempData["user"].ShouldEqual(new FakeItem() { Name = "User" + i });
                });
            });

            await Task.WhenAll(tasks);
        }
Example #3
0
        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind <ConnectionMultiplexer>()
            .ToMethod(ctx => ConnectionMultiplexer.Connect("localhost"))
            .InSingletonScope();

            kernel.Bind <IDatabase>()
            .ToMethod(ctx => ctx.Kernel.Get <ConnectionMultiplexer>().GetDatabase(0))
            .InRequestScope();

            // Besure to override CreateTempDataProvider so that DependencyResolver
            // behaves as expected. See this bug in MVC >= 4:
            // https://aspnetwebstack.codeplex.com/workitem/1692
            kernel.Bind <ITempDataProvider>()
            .ToMethod(ctx =>
            {
                var options = new RedisTempDataProviderOptions()
                {
                    KeyPrefix    = "MyTempData",
                    KeySeparator = "/",
                    // Serializer = new CustomTempDataSerializer(),
                    // UserProvider = new CustomUserProvider()
                };

                return(new RedisTempDataProvider(options, ctx.Kernel.Get <IDatabase>()));
            })
            .InRequestScope();
        }
        static RedisTempDataProviderTests()
        {
            options = new RedisTempDataProviderOptions()
            {
                KeyPrefix    = prefix,
                KeySeparator = separator,
                UserProvider = new FakeTempDataUserProvider(user),
                Serializer   = new NetDataContractTempDataSerializer()
            };

            context = new ControllerContext();
        }
        static RedisTempDataProviderTests()
        {
            options = new RedisTempDataProviderOptions()
            {
                KeyPrefix = prefix,
                KeySeparator = separator,
                UserProvider = new FakeTempDataUserProvider(user),
                Serializer = new FakeTempDataSerializer()
            };

            context = new ControllerContext();
        }
        private void RegisterRedisTempDataProvider(Container container)
        {
            // You could also use ConfigurationOptions object for Redis connection configuration
            const string ServerName    = "localhost";
            const int    DatabaseIndex = 0;

            var redisConnectionMultiplexer = ConnectionMultiplexer.Connect(ServerName);

            container.RegisterSingleton(() => redisConnectionMultiplexer);
            container.RegisterPerWebRequest(() => container.GetInstance <ConnectionMultiplexer>().GetDatabase(DatabaseIndex));
            container.RegisterPerWebRequest <ITempDataProvider>(() =>
            {
                var options = new RedisTempDataProviderOptions
                {
                    KeyPrefix    = "__TempData",
                    KeySeparator = "/"
                };

                return(new RedisTempDataProvider(options, container.GetInstance <IDatabase>()));
            });
        }