Beispiel #1
0
        private async Task <List <Friend> > GetFriends(ulong steamId)
        {
            var memoryCache         = new MemoryCache(new MemoryCacheOptions());
            var memoryCacheProvider = new MemoryCacheProvider(memoryCache);
            var ttl         = new AbsoluteTtl(DateTimeOffset.Now.Date.AddMinutes(5));
            var cachePolicy = Policy.CacheAsync(memoryCacheProvider, ttl, cxt => $"{cxt.OperationKey}:{steamId}");

            var retryPolicy = Policy
                              .Handle <Exception>()
                              .WaitAndRetryAsync(new[]
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(2),
                TimeSpan.FromSeconds(3)
            });

            var commonResilience = Policy.WrapAsync(cachePolicy, retryPolicy);

            var friends = await Policy <List <Friend> >
                          .Handle <Exception>()
                          .FallbackAsync(new List <Friend>())
                          .WrapAsync(commonResilience)
                          .ExecuteAsync(() => dotaClient.GetFriendsList(steamId));

            return(friends);
        }
Beispiel #2
0
        private async Task <List <Profile> > GetProfiles(int key, params ulong[] identities)
        {
            var memoryCache         = new MemoryCache(new MemoryCacheOptions());
            var memoryCacheProvider = new MemoryCacheProvider(memoryCache);
            var ttl         = new AbsoluteTtl(DateTimeOffset.Now.Date.AddMinutes(5));
            var cachePolicy = Policy.CacheAsync(memoryCacheProvider, ttl, cxt => $"{cxt.OperationKey}:{key}");

            var retryPolicy = Policy
                              .Handle <Exception>()
                              .WaitAndRetryAsync(new[]
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(10)
            });

            var commonResilience = Policy.WrapAsync(cachePolicy, retryPolicy);

            var profiles = await Policy <List <Profile> >
                           .Handle <Exception>()
                           .FallbackAsync(new List <Profile>())
                           .WrapAsync(commonResilience)
                           .ExecuteAsync(() => dotaClient.GetPlayersSummary(identities.ToList()));

            return(profiles);
        }
Beispiel #3
0
        public void Should_return_timespan_reflecting_time_until_expiry()
        {
            DateTime today    = DateTime.Today;
            DateTime tomorrow = today.AddDays(1);

            AbsoluteTtl ttlStrategy = new AbsoluteTtl(tomorrow);

            SystemClock.DateTimeOffsetUtcNow = () => today;
            ttlStrategy.GetTtl(new Context("someExecutionKey"), null).Timespan.Should().Be(TimeSpan.FromDays(1));
        }
Beispiel #4
0
        public void Should_return_zero_ttl_if_configured_to_expire_in_past()
        {
            AbsoluteTtl ttlStrategy = new AbsoluteTtl(SystemClock.DateTimeOffsetUtcNow().Subtract(TimeSpan.FromTicks(1)));

            ttlStrategy.GetTtl(new Context("someExecutionKey"), null).Timespan.Should().Be(TimeSpan.Zero);
        }