예제 #1
0
        public async Task CachingShouldBeSuccess()
        {
            const int taskValue = 10;

            int counter = 0;

            var cachedTask = new CachedTask <int>(() =>
            {
                counter++;
                if (counter == 1)
                {
                    return(Task.FromResult(taskValue));
                }
                return(Task.FromException <int>(new Exception()));
            }, TimeSpan.FromMilliseconds(100), CacheFallbackPolicy.LeavePreviousSuccessValue);


            await cachedTask.GetResult();

            await cachedTask.GetResult();

            await cachedTask.GetResult();

            await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);

            var x = await cachedTask.GetResult();

            x.Should().Be(taskValue);
        }
예제 #2
0
        protected override CachedTask <EventPoolResponse> GetData(bool force, CancellationToken token)
        {
            if (force ||
                Pool == null ||
                (Pool != null && Pool.AlwaysRefresh == true) ||
                (_previousSettings.Item1 != _settings.SearchPeriod || _previousSettings.Item2 != _settings.SearchInPast))
            {
                if (!_settings.ExcludedCategoriesByPool.ContainsKey(_poolId))
                {
                    _settings.ExcludedCategoriesByPool.Add(_poolId, new int[0]);
                }
                if (!_settings.ExcludedTagsByPool.ContainsKey(_poolId))
                {
                    _settings.ExcludedTagsByPool.Add(_poolId, new string[0]);
                }

                var request = new EventPoolRequest
                {
                    PoolId           = _poolId,
                    HoursCount       = (int)_settings.SearchPeriod,
                    IsInPast         = _settings.SearchInPast,
                    UserTickets      = _settings.UserTickets.ToArray(),
                    FavoriteEventIds = _settings.FavoriteItemIds.ToArray(),
                    Language         = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName
                };
                return(CachedTask.Create(() => _eventsService.GetEventPoolAsync(request, token), _poolId, DateTime.Now.Add(CacheDuration)));
            }
            return(CachedTask.NoNewData <EventPoolResponse>());
        }
예제 #3
0
 protected override CachedTask <BeersResponse> GetData(bool force, CancellationToken token)
 {
     if (force)
     {
         return(CachedTask.Create(() => _beerService.GetBeersAsync(token)));
     }
     return(CachedTask.NoNewData <BeersResponse>());
 }
예제 #4
0
        protected override CachedTask <FeedsResponse> GetData(bool force, CancellationToken token)
        {
            if (!force)
            {
                return(CachedTask.NoNewData <FeedsResponse>());
            }

            var request = new FeedsRequest
            {
                Language           = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName,
                IncludeGeneralFeed = true
            };

            return(CachedTask.Create(() => _feedsService.GetFeedsAsync(request, token)));
        }
예제 #5
0
        protected override CachedTask <FeedItemContentResponse> GetData(bool force, CancellationToken token)
        {
            if (!force)
            {
                return(CachedTask.NoNewData <FeedItemContentResponse>());
            }

            var request = new FeedItemContentRequest
            {
                Language = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName,
                ItemId   = Item.Id
            };

            return(CachedTask.Create(() => _newsService.GetFeedItemContentAsync(request, token), Item.Id, DateTime.Now.Add(CacheDuration)));
        }
예제 #6
0
        protected override CachedTask <EventItemResponse> GetData(bool force, CancellationToken token)
        {
            if (force)
            {
                var request = new EventItemRequest
                {
                    ItemId      = _itemId,
                    UserTickets = _settings.UserTickets.ToArray(),
                    Language    = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName
                };

                return(CachedTask.Create(() => _eventsService.GetEventItemAsync(request, token), _itemId, DateTime.Now.Add(CacheDuration)));
            }

            return(CachedTask.NoNewData <EventItemResponse>());
        }
예제 #7
0
        protected override CachedTask <CoursesResponse> GetData(bool force, CancellationToken token)
        {
            if (!force)
            {
                return(CachedTask.NoNewData <CoursesResponse>());
            }

            return(CachedTask.Create(() => _requestHandler.ExecuteAsync(() =>
            {
                var request = new CoursesRequest
                {
                    Language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName
                };
                return _moodleService.GetCoursesAsync(request, token);
            })));
        }
예제 #8
0
        protected override CachedTask <FoodResponse> GetData(bool force, CancellationToken token)
        {
            if (!force)
            {
                return(CachedTask.NoNewData <FoodResponse>());
            }

            var request = new FoodRequest
            {
                Language = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName,
                MealTime = MealTime,
                Date     = MealDate,
                UserName = _credentials.UserName
            };

            Func <Task <FoodResponse> > getter = () => _menuService.GetMenusAsync(request, token);

            if (MealDate.Date == DateTime.Now.Date)
            {
                return(CachedTask.Create(getter, MealTime.GetHashCode(), DateTime.Now.Date.AddDays(1)));
            }
            return(CachedTask.DoNotCache(getter));
        }
예제 #9
0
        protected override CachedTask <ScheduleResponse> GetData(bool force, CancellationToken token)
        {
            if (!force)
            {
                return(CachedTask.NoNewData <ScheduleResponse>());
            }

            Func <Task <ScheduleResponse> > getter = () => _requestHandler.ExecuteAsync(() =>
            {
                var request = new ScheduleRequest
                {
                    Language  = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName,
                    WeekStart = WeekDate
                };
                return(_isaService.GetScheduleAsync(request, token));
            });

            if (DateTime.Now >= WeekDate && (DateTime.Now - WeekDate).TotalDays < DaysInWeek)
            {
                // cache ID must be non-zero so that other requests don't think this can be used as a placeholder
                return(CachedTask.Create(getter, 1, WeekDate.AddDays(DaysInWeek)));
            }
            return(CachedTask.DoNotCache(getter));
        }
예제 #10
0
 public void Add <taskType>(CachedTask <taskType> cachedTask, Action runTask) where taskType : class
 {
 }
예제 #11
0
 public void Add <taskType>(CachedTask <taskType> cachedTask, Action runTask, Func <bool> condition, FlowMode flowMode) where taskType : class
 {
 }
예제 #12
0
 public void Add <controller>(CachedTask <controller> cachedTask, Action runTask, FlowMode flowMode) where controller : class
 {
 }
예제 #13
0
 public void Add <controller>(CachedTask <controller> cachedTask, Action runTask, Direction direction) where controller : class
 {
 }
예제 #14
0
 public void Add <controller>(CachedTask <controller> cachedTask, Action runTask, Func <bool> condition) where controller : class
 {
 }