Ejemplo n.º 1
0
        public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            IDataSerializer serializer;

            if (ConfigurationManager.AppSettings["serializer"] != null &&
                ConfigurationManager.AppSettings["serializer"] == "redis")
            {
                var connection = ConnectionMultiplexer.Connect("voiceflows.redis.cache.windows.net, password=0DF8T+8zJ77KWJYar33aqSFgeG/NTjtqMo5oJU1b3n4=");
                serializer = new RedisSerializer {
                    Cache = connection.GetDatabase()
                };
            }
            else
            {
                serializer = new MemorySerializer();
            }

            container.Register(Component.For <WorkflowEngine>()
                               .Instance(WorkflowSetup.Configure(typeof(JaviCustomWorkflow).Assembly)
                                         .WithSerializer(serializer)
                                         .WithCastleWindsor(container).WorkflowEngine));

            container.Register(Component.For <IDataSerializer>().Instance(serializer));
        }
Ejemplo n.º 2
0
        }                                                                       //channel, type, message

        public UdpPortSubscriber(string ip, int port, ILogger logger, Action <string, string, string> onMessage)
        {
            _port       = port;
            _logger     = logger;
            OnMessage   = onMessage;
            _ip         = ip;
            _serializer = new RedisSerializer();
            //_customChannel = channel ?? PubChannel;
        }
Ejemplo n.º 3
0
        public UdpPublisher(string ip, int port, string channel, ILogger logger)
        {
            _ip     = ip;
            _port   = port;
            _logger = logger;

            _serializer    = new RedisSerializer();
            _customChannel = channel ?? PubChannel;
        }
Ejemplo n.º 4
0
        public async Task Should_Not_Request_Again_For_Same_Context_Async()
        {
            var context1 = GetNewContextSubstitute();
            var context2 = GetNewContextSubstitute();

            string cacheKey   = Guid.NewGuid().ToString();
            int    counter    = 0;
            int    cacheValue = 1;

            Task <TestCacheItem> GetCacheValue()
            {
                counter++;
                return(Task.FromResult(new TestCacheItem {
                    Value = cacheValue
                }));
            }

            CurrentHttpContext = context1;
            var item1 = await _typedCache.GetAsync(cacheKey, GetCacheValue); //First request

            CurrentHttpContext = context2;
            var item2 = await _typedCache.GetAsync(cacheKey, GetCacheValue); //Second request

            CurrentHttpContext = context1;
            var item3 = await _typedCache.GetAsync(cacheKey, GetCacheValue); //First request again

            CurrentHttpContext = context2;
            var item4 = await _typedCache.GetAsync(cacheKey, GetCacheValue); //Second request again

            await RedisDatabase.Received(2).StringGetAsync(Arg.Any <RedisKey>());

            var cachedObject =
                RedisSerializer.Serialize(new TestCacheItem {
                Value = cacheValue
            }, typeof(TestCacheItem));

            await RedisDatabase.Received(2).StringSetAsync(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(),
                                                           Arg.Any <When>(),
                                                           Arg.Any <CommandFlags>());

            (await _typedCache.GetOrDefaultAsync(cacheKey)).Value.ShouldBe(cacheValue);

            counter.ShouldBe(2);
            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);

            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);

            item3.ShouldNotBe(null);
            item3.Value.ShouldBe(cacheValue);

            item4.ShouldNotBe(null);
            item4.Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 5
0
        public async Task Should_Request_Again_For_Different_Contexts_Async()
        {
            RedisDatabase.ClearReceivedCalls();

            string cacheKey   = "Test";
            int    cacheValue = 1;

            int counter = 0;

            Task <MyCacheItem> GetCacheValue()
            {
                counter++;
                return(Task.FromResult(new MyCacheItem {
                    Value = cacheValue
                }));
            }

            RedisDatabase.StringSetAsync(Arg.Any <RedisKey>(), Arg.Any <RedisValue>(), Arg.Any <TimeSpan>()).Returns(true);

            var cachedObject = RedisSerializer.Serialize(new MyCacheItem {
                Value = cacheValue
            }, typeof(MyCacheItem));

            ChangeHttpContext();

            var item = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            await RedisDatabase.Received(1).StringGetAsync(Arg.Any <RedisKey>());

            await RedisDatabase.Received(1).StringSetAsync(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            RedisDatabase.StringGetAsync(Arg.Any <RedisKey>()).Returns(Task.FromResult(cachedObject));

            ChangeHttpContext();
            var item1 = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            await RedisDatabase.Received(2).StringGetAsync(Arg.Any <RedisKey>());

            ChangeHttpContext();
            var item2 = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            await RedisDatabase.Received(3).StringGetAsync(Arg.Any <RedisKey>());

            //should still be one received calls
            await RedisDatabase.Received(1).StringSetAsync(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            counter.ShouldBe(1);
            item.ShouldNotBe(null);
            item.Value.ShouldBe(cacheValue);
            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);
            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 6
0
        public void Should_Not_Change_Normal_Redis_Cache()
        {
            RedisDatabase.ClearReceivedCalls();

            string cacheKey   = "Test";
            int    cacheValue = 1;

            int counter = 0;

            MyCacheItem GetCacheValue()
            {
                counter++;
                return(new MyCacheItem {
                    Value = cacheValue
                });
            }

            RedisDatabase.StringSet(Arg.Any <RedisKey>(), Arg.Any <RedisValue>(), Arg.Any <TimeSpan>()).Returns(true);

            var cachedObject = RedisSerializer.Serialize(new MyCacheItem {
                Value = cacheValue
            }, typeof(MyCacheItem));

            var item = _normalRedisCache.Get(cacheKey, GetCacheValue);

            RedisDatabase.Received(2).StringGet(Arg.Any <RedisKey>()); //redis cache tries to get value two times if value not exists see AbpCacheBase<TKey, TValue>.Get(TKey key, Func<TKey, TValue> factory)
            RedisDatabase.Received(1).StringSet(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            RedisDatabase.StringGet(Arg.Any <RedisKey>()).Returns(cachedObject);

            var item1 = _normalRedisCache.Get(cacheKey, GetCacheValue);

            RedisDatabase.Received(3).StringGet(Arg.Any <RedisKey>());

            var item2 = _normalRedisCache.Get(cacheKey, GetCacheValue);

            RedisDatabase.Received(4).StringGet(Arg.Any <RedisKey>());

            //should still be one received calls
            RedisDatabase.Received(1).StringSet(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            counter.ShouldBe(1);
            item.ShouldNotBe(null);
            item.Value.ShouldBe(cacheValue);
            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);
            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);

            _normalRedisCache.GetOrDefault(cacheKey).Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 7
0
        public void Should_Work_With_Null_Contexts()
        {
            CurrentHttpContext = null;
            RedisDatabase.ClearReceivedCalls();

            string cacheKey   = "Test";
            int    cacheValue = 1;

            int counter = 0;

            MyCacheItem GetCacheValue()
            {
                counter++;
                return(new MyCacheItem {
                    Value = cacheValue
                });
            }

            RedisDatabase.StringSet(Arg.Any <RedisKey>(), Arg.Any <RedisValue>(), Arg.Any <TimeSpan>()).Returns(true);

            var cachedObject = RedisSerializer.Serialize(new MyCacheItem {
                Value = cacheValue
            }, typeof(MyCacheItem));

            var item = _perRequestRedisCache.Get(cacheKey, GetCacheValue);

            RedisDatabase.Received(2).StringGet(Arg.Any <RedisKey>());
            RedisDatabase.Received(1).StringSet(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            RedisDatabase.StringGet(Arg.Any <RedisKey>()).Returns(cachedObject);

            var item1 = _perRequestRedisCache.Get(cacheKey, GetCacheValue);

            RedisDatabase.Received(3).StringGet(Arg.Any <RedisKey>());      //since _currentHttpContext is null it should go to the redisdb again

            var item2 = _perRequestRedisCache.Get(cacheKey, GetCacheValue); //since _currentHttpContext is null it should go to the redisdb again

            RedisDatabase.Received(4).StringGet(Arg.Any <RedisKey>());

            //should still be one received calls
            RedisDatabase.Received(1).StringSet(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            counter.ShouldBe(1);
            item.ShouldNotBe(null);
            item.Value.ShouldBe(cacheValue);
            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);
            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 8
0
        public Task PublishLatestAsync <TKey, TEntry>(string id, TEntry entry)
            where TEntry : IRedisEntry
        {
            if (_connection == null)
            {
                throw new InvalidOperationException("The redis connection has not been started.");
            }

            var data = RedisSerializer.Serialize <TKey, TEntry>(id, entry);

            var key   = CreateSubscriptionKey(id, SubscriptionType.LatestPerCollection);
            var ticks = entry.GetTimestamp().Ticks;

            return(_connection.GetDatabase(0)
                   .ScriptEvaluateAsync(_publishLatestScript.Hash, new RedisKey[] { key }, new RedisValue[] { ticks, data }));
        }
Ejemplo n.º 9
0
        public UdpSingletonPublisher(string ip, int port, string channel, ILogger logger) //
        {
            _ip     = ip;
            _port   = port;
            _logger = logger;

            _serializer    = new RedisSerializer();
            _customChannel = channel ?? PubChannel;

            _udpClient = new UdpClient(AddressFamily.InterNetwork);
            _udpClient.AllowNatTraversal(true);
            var address = IPAddress.Parse(_ip);

            _ipEndPoint = new IPEndPoint(address, _port);
            _udpClient.JoinMulticastGroup(address);
        }
Ejemplo n.º 10
0
        public Task PublishAllAsync <TKey, TEntry>(string id, IEnumerable <TEntry> entries)
            where TEntry : IRedisEntry
        {
            if (_connection == null)
            {
                throw new InvalidOperationException("The redis connection has not been started.");
            }

            var key    = CreateSubscriptionKey(id, SubscriptionType.AllFromCollections);
            var arrays = RedisSerializer.Serialize <TKey, TEntry>(id, entries.ToList(), 64 * 1024);
            var tasks  = new List <Task>();

            foreach (var data in arrays)
            {
                tasks.Add(_redisSubscriber.PublishAsync(key, data));
            }
            return(Task.WhenAll(tasks));
        }
Ejemplo n.º 11
0
        public async Task Simple_Get_Set_Test_Async(string cacheKey, int cacheValue)
        {
            var item = await _perRequestRedisCache.GetAsync(cacheKey, () => Task.FromResult(new MyCacheItem {
                Value = cacheValue
            }));

            item.ShouldNotBe(null);
            item.Value.ShouldBe(cacheValue);

            var cachedObject = RedisSerializer.Serialize(new MyCacheItem {
                Value = cacheValue
            }, typeof(MyCacheItem));

            RedisDatabase.StringGetAsync(Arg.Any <RedisKey>()).Returns(Task.FromResult(cachedObject));
            await RedisDatabase.Received().StringGetAsync(Arg.Any <RedisKey>());

            (await _perRequestRedisCache.GetOrDefaultAsync(cacheKey)).Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 12
0
        public void Simple_Get_Set_Test(string cacheKey, int cacheValue)
        {
            var item = _perRequestRedisCache.Get(cacheKey, () => new MyCacheItem {
                Value = cacheValue
            });

            item.ShouldNotBe(null);
            item.Value.ShouldBe(cacheValue);

            var cachedObject = RedisSerializer.Serialize(new MyCacheItem {
                Value = cacheValue
            }, typeof(MyCacheItem));

            RedisDatabase.StringGet(Arg.Any <RedisKey>()).Returns(cachedObject);
            RedisDatabase.Received().StringGet(Arg.Any <RedisKey>());

            _perRequestRedisCache.GetOrDefault(cacheKey).Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 13
0
        public void Should_Request_Again_For_Same_Context()
        {
            ChangeHttpContext();

            string cacheKey   = Guid.NewGuid().ToString();
            int    counter    = 0;
            int    cacheValue = 1;

            TestCacheItem GetCacheValue()
            {
                counter++;
                return(new TestCacheItem {
                    Value = cacheValue
                });
            }

            var item1 = _typedCache.Get(cacheKey, GetCacheValue);

            ChangeHttpContext();
            var item2 = _typedCache.Get(cacheKey, GetCacheValue);

            RedisDatabase.Received(2).StringGet(Arg.Any <RedisKey>());

            var cachedObject =
                RedisSerializer.Serialize(new TestCacheItem {
                Value = cacheValue
            }, typeof(TestCacheItem));

            RedisDatabase.Received(2).StringSet(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(),
                                                Arg.Any <CommandFlags>());

            _typedCache.GetOrDefault(cacheKey).Value.ShouldBe(cacheValue);

            counter.ShouldBe(2);

            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);

            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 14
0
        public async Task Should_Request_Once_For_Same_Context_Async()
        {
            RedisDatabase.ClearReceivedCalls();

            string cacheKey   = "Test";
            int    cacheValue = 1;

            int counter = 0;

            Task <MyCacheItem> GetCacheValue()
            {
                counter++;
                return(Task.FromResult(new MyCacheItem {
                    Value = cacheValue
                }));
            }

            var item = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            var item1 = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            var item2 = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            counter.ShouldBe(1);
            item.ShouldNotBe(null);
            item.Value.ShouldBe(cacheValue);
            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);
            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);

            await RedisDatabase.Received(1).StringGetAsync(Arg.Any <RedisKey>());

            var cachedObject = RedisSerializer.Serialize(new MyCacheItem {
                Value = cacheValue
            }, typeof(MyCacheItem));
            await RedisDatabase.Received(1).StringSetAsync(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            (await _perRequestRedisCache.GetOrDefaultAsync(cacheKey)).Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 15
0
        public void Should_Request_Once_For_Same_Context()
        {
            RedisDatabase.ClearReceivedCalls();

            string cacheKey   = "Test";
            int    cacheValue = 1;

            int counter = 0;

            MyCacheItem GetCacheValue()
            {
                counter++;
                return(new MyCacheItem {
                    Value = cacheValue
                });
            }

            var item  = _perRequestRedisCache.Get(cacheKey, GetCacheValue);
            var item1 = _perRequestRedisCache.Get(cacheKey, GetCacheValue);
            var item2 = _perRequestRedisCache.Get(cacheKey, GetCacheValue);

            counter.ShouldBe(1);
            item.ShouldNotBe(null);
            item.Value.ShouldBe(cacheValue);
            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);
            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);

            RedisDatabase.Received(1).StringGet(Arg.Any <RedisKey>());

            var cachedObject = RedisSerializer.Serialize(new MyCacheItem {
                Value = cacheValue
            }, typeof(MyCacheItem));

            RedisDatabase.Received(1).StringSet(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            _perRequestRedisCache.GetOrDefault(cacheKey).Value.ShouldBe(cacheValue);
        }
Ejemplo n.º 16
0
        public Task SubscribeAsync <TKey, TEntry>(string id, IKeyConverter <TKey> keyConverter, SubscriptionType subscribe, Action <Serie <TKey, TEntry> > onMessage)
            where TEntry : IRedisEntry, new()
        {
            if (_redisSubscriber == null)
            {
                throw new InvalidOperationException("The redis connection has not been started.");
            }

            var key = CreateSubscriptionKey(id, subscribe);

            return(_redisSubscriber.SubscribeAsync(key, async(channel, data) =>
            {
                try
                {
                    var entries = await RedisSerializer.Deserialize <TKey, TEntry>(data, keyConverter).ConfigureAwait(false);
                    onMessage(entries);
                }
                catch (Exception e)
                {
                    _logger.Error(e, "An error ocurred while deserializing subscription message.");
                }
            }));
        }