public async Task Should_allow_custom_ICacheKeyStrategy()
        {
            Action <Context, string, Exception> noErrorHandling = (_, __, ___) => { };
            Action <Context, string>            emptyDelegate   = (_, __) => { };

            IAsyncCacheProvider stubCacheProvider = new StubCacheProvider();
            ICacheKeyStrategy   cacheKeyStrategy  = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
            //var cache = Policy.CacheAsync<ResultClass>(stubCacheProvider, TimeSpan.MaxValue, cacheKeyStrategy);
            var cache = Policy.CacheAsync <ResultClass>(stubCacheProvider.AsyncFor <ResultClass>(), new RelativeTtl(TimeSpan.MaxValue), cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, noErrorHandling, noErrorHandling);

            object person1 = new ResultClass(ResultPrimitive.Good, "person1");
            await stubCacheProvider.PutAsync("person1", person1, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false).ConfigureAwait(false);

            object person2 = new ResultClass(ResultPrimitive.Good, "person2");
            await stubCacheProvider.PutAsync("person2", person2, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false).ConfigureAwait(false);

            bool funcExecuted = false;
            Func <Context, Task <ResultClass> > func = async ctx => { funcExecuted = true; await TaskHelper.EmptyTask.ConfigureAwait(false); return(new ResultClass(ResultPrimitive.Fault, "should never return this one")); };

            (await cache.ExecuteAsync(func, new Context("person", new { id = "1" }.AsDictionary())).ConfigureAwait(false)).Should().BeSameAs(person1);
            funcExecuted.Should().BeFalse();

            (await cache.ExecuteAsync(func, new Context("person", new { id = "2" }.AsDictionary())).ConfigureAwait(false)).Should().BeSameAs(person2);
            funcExecuted.Should().BeFalse();
        }
Exemplo n.º 2
0
        public void Should_allow_custom_ICacheKeyStrategy()
        {
            Action <Context, string, Exception> noErrorHandling = (_, __, ___) => { };
            Action <Context, string>            emptyDelegate   = (_, __) => { };

            ISyncCacheProvider        stubCacheProvider = new StubCacheProvider();
            ICacheKeyStrategy         cacheKeyStrategy  = new StubCacheKeyStrategy(context => context.ExecutionKey + context["id"]);
            CachePolicy <ResultClass> cache             = Policy.Cache <ResultClass>(stubCacheProvider.For <ResultClass>(), new RelativeTtl(TimeSpan.MaxValue), cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, noErrorHandling, noErrorHandling);

            object person1 = new ResultClass(ResultPrimitive.Good, "person1");

            stubCacheProvider.Put("person1", person1, new Ttl(TimeSpan.MaxValue));
            object person2 = new ResultClass(ResultPrimitive.Good, "person2");

            stubCacheProvider.Put("person2", person2, new Ttl(TimeSpan.MaxValue));

            bool funcExecuted       = false;
            Func <ResultClass> func = () => { funcExecuted = true; return(new ResultClass(ResultPrimitive.Fault, "should never return this one")); };

            cache.Execute(func, new Context("person", new { id = "1" }.AsDictionary())).Should().BeSameAs(person1);
            funcExecuted.Should().BeFalse();

            cache.Execute(func, new Context("person", new { id = "2" }.AsDictionary())).Should().BeSameAs(person2);
            funcExecuted.Should().BeFalse();
        }
Exemplo n.º 3
0
        public void Should_allow_custom_ICacheKeyStrategy()
        {
            Action <Context, string, Exception> noErrorHandling = (_, __, ___) => { };
            Action <Context, string>            emptyDelegate   = (_, __) => { };

            ISyncCacheProvider stubCacheProvider = new StubCacheProvider();
            ICacheKeyStrategy  cacheKeyStrategy  = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
            CachePolicy        cache             = Policy.Cache(stubCacheProvider, new RelativeTtl(TimeSpan.MaxValue), cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, noErrorHandling, noErrorHandling);

            object person1 = new object();

            stubCacheProvider.Put("person1", person1, new Ttl(TimeSpan.MaxValue));
            object person2 = new object();

            stubCacheProvider.Put("person2", person2, new Ttl(TimeSpan.MaxValue));

            bool funcExecuted           = false;
            Func <Context, object> func = ctx => { funcExecuted = true; return(new object()); };

            cache.Execute(func, new Context("person", new { id = "1" }.AsDictionary())).Should().BeSameAs(person1);
            funcExecuted.Should().BeFalse();

            cache.Execute(func, new Context("person", new { id = "2" }.AsDictionary())).Should().BeSameAs(person2);
            funcExecuted.Should().BeFalse();
        }
Exemplo n.º 4
0
        public async Task Should_allow_custom_ICacheKeyStrategy()
        {
            Action <Context, string, Exception> noErrorHandling = (_, _, _) => { };
            Action <Context, string>            emptyDelegate   = (_, _) => { };

            IAsyncCacheProvider stubCacheProvider = new StubCacheProvider();
            ICacheKeyStrategy   cacheKeyStrategy  = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
            var cache = Policy.CacheAsync(stubCacheProvider, new RelativeTtl(TimeSpan.MaxValue), cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, noErrorHandling, noErrorHandling);

            object person1 = new object();
            await stubCacheProvider.PutAsync("person1", person1, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false);

            object person2 = new object();
            await stubCacheProvider.PutAsync("person2", person2, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false);

            bool funcExecuted = false;
            Func <Context, Task <object> > func = async _ => { funcExecuted = true; await TaskHelper.EmptyTask; return(new object()); };

            (await cache.ExecuteAsync(func, new Context("person", new { id = "1" }.AsDictionary()))).Should().BeSameAs(person1);
            funcExecuted.Should().BeFalse();

            (await cache.ExecuteAsync(func, new Context("person", new { id = "2" }.AsDictionary()))).Should().BeSameAs(person2);
            funcExecuted.Should().BeFalse();
        }