Exemplo n.º 1
0
        public async Task GetSetAsync_calls_calculateValue_on_miss()
        {
            // Arrange
            string?  setKey        = null;
            string?  setValue      = null;
            TimeSpan?setExpiration = default;

            var multiplexer = FakeRedis.CreateConnectionMultiplexer(onSet: (key, value, expiration) =>
            {
                setKey        = key;
                setValue      = value;
                setExpiration = expiration;
            });

            var sut    = new RedisCache(multiplexer, "prefix");
            var called = false;

            // Act
            var valueFromCache = await sut.GetSetAsync("testKey", async ct => { called = true; return("42"); }, TimeSpan.FromSeconds(10));

            // Assert
            Assert.True(called);
            Assert.Equal("42", valueFromCache);
            Assert.Equal("prefix:testKey", setKey);
            Assert.Equal("\"42\"", setValue); // Json-encoded
            Assert.Equal(TimeSpan.FromSeconds(10), setExpiration);
        }
Exemplo n.º 2
0
        public void GetSet_uses_cached_protobuf_object_on_hit()
        {
            // Arrange
            string?lookupKey = null;
            var    entity    = new Entity {
                IntProp = 5
            };
            var protoSerializer = new ProtobufSerializer();

            var multiplexer = FakeRedis.CreateConnectionMultiplexer(onGet: key => { lookupKey = key; return(protoSerializer.Serialize(entity)); });

            var sut = new RedisCache(multiplexer, "prefix")
            {
                Serializer = protoSerializer
            };
            var called = false;

            // Act
            var valueFromCache = sut.GetSet("testKey", () => { called = true; return(new Entity()); }, TimeSpan.FromSeconds(10));

            // Assert
            Assert.False(called);
            Assert.NotNull(valueFromCache);
            Assert.Equal(5, entity.IntProp);
            Assert.Equal("prefix:testKey", lookupKey);
        }
Exemplo n.º 3
0
        public async Task GetSetAsync_always_calculates_the_value()
        {
            // Arrange
            var subscriber = FakeRedis.CreateSubscriber();
            var sut        = new RedisInvalidationSender(subscriber, "invalidation");

            var count = 0;

            // Act
            var valueFromCache = await sut.GetSetAsync("testKey", async ct => { ++count; return("42"); }, TimeSpan.FromSeconds(10));

            // Assert
            Assert.Equal(1, count);
            Assert.Equal("42", valueFromCache);
        }
Exemplo n.º 4
0
        public void Invalidation_messages_call_Invalidate_on_inner_cache()
        {
            // Arrange
            string?invalidatedKey = null;
            var    innerCache     = new InspectableCache(key => { invalidatedKey = key; });

            var subscriber = FakeRedis.CreateSubscriber();

            var sut = new RedisInvalidationReceiver(innerCache, subscriber, "invalidation");

            // Act
            subscriber.Publish("invalidation", "testKey");

            // Assert
            Assert.Equal("testKey", invalidatedKey);
        }
Exemplo n.º 5
0
        public async Task GetSetAsync_uses_cached_value_on_hit()
        {
            // Arrange
            string?lookupKey = null;

            var multiplexer = FakeRedis.CreateConnectionMultiplexer(onGet: key => { lookupKey = key; return("\"42\""); });

            var sut    = new RedisCache(multiplexer, "prefix");
            var called = false;

            // Act
            var valueFromCache = await sut.GetSetAsync("testKey", async ct => { called = true; return("not 42"); }, TimeSpan.FromSeconds(10));

            // Assert
            Assert.False(called);
            Assert.Equal("42", valueFromCache);
            Assert.Equal("prefix:testKey", lookupKey);
        }
Exemplo n.º 6
0
        public async Task InvalidateAsync_publishes_an_invalidation_message()
        {
            // Arrange
            string?publishedChannel = null;
            string?publishedMessage = null;

            var subscriber = FakeRedis.CreateSubscriber(onPublish: (c, m) =>
            {
                publishedChannel = c;
                publishedMessage = m;
            });

            var sut = new RedisInvalidationSender(subscriber, "invalidation");

            // Act
            await sut.InvalidateAsync("testKey");

            // Assert
            Assert.Equal("invalidation", publishedChannel);
            Assert.Equal("testKey", publishedMessage);
        }
Exemplo n.º 7
0
        public async Task InvalidateAsync_clears_the_value()
        {
            // Arrange
            string?    setKey        = null;
            RedisValue setValue      = default;
            TimeSpan?  setExpiration = default;

            var multiplexer = FakeRedis.CreateConnectionMultiplexer(onSet: (key, value, expiration) =>
            {
                setKey        = key;
                setValue      = value;
                setExpiration = expiration;
            });

            var sut = new RedisCache(multiplexer, "prefix");

            // Act
            await sut.InvalidateAsync("testKey");

            // Assert
            Assert.Equal("prefix:testKey", setKey);
            Assert.True(setValue.IsNull);
            Assert.Null(setExpiration);
        }