public void OnMessage_EventTriggered(string channelName)
        {
            Action <RedisChannel, RedisValue> method = null;

            A.CallTo(() => _subscriber.Subscribe(
                         channelName,
                         A <Action <RedisChannel, RedisValue> > ._,
                         A <CommandFlags> ._)).Invokes(i => method = i.GetArgument <Action <RedisChannel, RedisValue> >(1));

            var eventHandler = A.Fake <EventHandler <CacheUpdateNotificationArgs> >();

            A.CallTo(() => _itemSerializer.Deserialize <CacheUpdateNotificationArgs>(A <byte[]> ._)).Returns(new CacheUpdateNotificationArgs()
            {
                ClientName = "A"
            });

            var cacheSubscriber = new RedisSubscriber(_connection, _remoteCache, _itemSerializer);

            cacheSubscriber.CacheUpdate += eventHandler;
            cacheSubscriber.CacheDelete += eventHandler;

            method.Invoke(channelName, "a");

            A.CallTo(() => eventHandler(A <object> .Ignored, A <CacheUpdateNotificationArgs> ._)).MustHaveHappened(Repeated.Exactly.Once);
        }
Example #2
0
        private void CacheUpdated(RedisChannel channel, RedisValue message)
        {
            var updateNotification = _itemSerializer.Deserialize <CacheUpdateNotificationArgs>(message);

            if (updateNotification.ClientName.Equals(_clientName))
            {
                return;
            }

            CacheUpdate?.Invoke(this, updateNotification);
        }
Example #3
0
        public T Get <T>(string key, Func <T> dataRetriever, TimeSpan?timeToLive) where T : class
        {
            var packedBytes = _database.StringGet(key);

            if (!packedBytes.IsNull)
            {
                return(_itemSerializer.Deserialize <T>(packedBytes));
            }

            var item = dataRetriever.Invoke();

            Add(key, item, timeToLive);
            return(item);
        }
Example #4
0
        public async Task <object> GetAsync(string key, Type type, Func <Task <object> > dataRetriever)
        {
            var packedBytes = await _database.StringGetAsync(key);

            if (!packedBytes.IsNull)
            {
                return(_itemSerializer.Deserialize(packedBytes, type));
            }

            var item = await dataRetriever.Invoke();

            if (item != null && item.GetType() == type)
            {
                Add(key, item);
                return(item);
            }

            return(null);
        }
Example #5
0
        public void RoundtripSerializeGeneric <T>(T input)
        {
            var result = serializer.Deserialize <T>(serializer.Serialize(input));

            result.ShouldBe(input);
        }