public async Task ShouldCacheWithKey()
        {
            // Arrange

            string key      = "key";
            string value    = "value";
            string newKey   = "newKey";
            string newValue = "newValue";
            var    fooQuery = new FooQuery {
                ValueToCache = value, CacheKey = key
            };

            var mock     = new Mock <IDistributedCache>();
            var sequence = new MockSequence();

            mock.InSequence(sequence).Setup(x => x.GetAsync($"FooQuery:{key}", default))
            .ReturnsAsync((byte[])null);
            mock.InSequence(sequence).Setup(x => x.GetAsync($"FooQuery:{key}", default))
            .ReturnsAsync(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(value)));
            mock.InSequence(sequence).Setup(x => x.GetAsync($"FooQuery:{newKey}", default))
            .ReturnsAsync(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(newValue)));
            var cache = mock.Object;

            var cacheQueryHandlerDecorator = new RedisCacheQueryHandlerDecorator <FooQuery, string>(new FooQueryHandler(), cache, queryInfoProvider);


            // Act
            var result = await cacheQueryHandlerDecorator
                         .Handle(fooQuery, Util.Context);

            // Assert
            Assert.Equal(result.Result, value);

            // Arrange
            // result should be old value for same key
            fooQuery.ValueToCache = newValue;

            // Act
            result = await cacheQueryHandlerDecorator
                     .Handle(fooQuery, Util.Context);

            // Assert
            Assert.Equal(result.Result, value);

            // result should be new value for new key

            // Arrange
            fooQuery.ValueToCache = newValue;
            fooQuery.CacheKey     = newKey;

            // Act
            result = await cacheQueryHandlerDecorator
                     .Handle(fooQuery, Util.Context);

            // Assert
            Assert.Equal(result.Result, newValue);
        }
예제 #2
0
 public FooCacheKeyProvider(FooQuery query, RequestContext context)
 {
     this.query = query;
 }