public async Task DataInMemoryCacheButNotFresh_DontRefreshData() { CachedItemWrapper <string> memoryCacheItem = new CachedItemWrapper <string>("fromMemCache", new DateTimeOffset(2020, 05, 17, 19, 59, 59, 00, new TimeSpan(0, 0, 0))); _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((true, memoryCacheItem)); _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0))); MemCacheFactory <string> memCacheFactory = new MemCacheFactory <string>(_pollySyncCacheProvider.Object, _mockableDateTime.Object, _logger.Object); IMemDistCache <string> memCache = memCacheFactory.GetCache(_defaultCacheDuration, ResetTimeFactory.OnHour); string result = await memCache.GetCachedDataAsync(_dataGetterDelegate1, _key, RefreshBehaviour.DontRefreshData, CancellationToken.None); Assert.AreEqual("fromMemCache", result); _pollySyncCacheProvider.Verify(x => x.TryGet(It.Is <string>(y => y == _key)), Times.Once); await Task.Delay(_waitForBackgroundThreadToCompleteMs); // wait for background thread Assert.AreEqual(0, _numberOfTimesDataGetterDelegate1Called); _pollySyncCacheProvider.Verify(x => x.Put(It.IsAny <string>(), It.IsAny <CachedItemWrapper <string> >(), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Never); }
public async Task DataInMemoryCacheButNotFresh_BackendGetErrors() { CachedItemWrapper <string> memoryCacheItem = new CachedItemWrapper <string>("fromMemCache", new DateTimeOffset(2020, 05, 17, 19, 59, 59, 00, new TimeSpan(0, 0, 0))); _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((true, memoryCacheItem)); _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0))); Func <CancellationToken, Task <string> > _dataGetterDelegate1 = (token) => { _numberOfTimesDataGetterDelegate1Called++; throw new Exception("An error"); }; MemCacheFactory <string> memCacheFactory = new MemCacheFactory <string>(_pollySyncCacheProvider.Object, _mockableDateTime.Object, _logger.Object); IMemDistCache <string> memCache = memCacheFactory.GetCache(_defaultCacheDuration, ResetTimeFactory.OnHour); string result = await memCache.GetCachedDataAsync(_dataGetterDelegate1, _key, RefreshBehaviour.DontWaitForFreshData, CancellationToken.None); Assert.AreEqual("fromMemCache", result); _pollySyncCacheProvider.Verify(x => x.TryGet(It.Is <string>(y => y == _key)), Times.Once); await Task.Delay(_waitForBackgroundThreadToCompleteMs); // wait for background thread _logger.Verify(x => x.LogError(It.Is <string>(y => y == $"Error executing data getter for key: {_key}"), It.IsAny <Exception>())); Assert.AreEqual(1, _numberOfTimesDataGetterDelegate1Called); DateTimeOffset whenDataWillNotBeFresh = new DateTimeOffset(2020, 05, 17, 21, 00, 00, 00, new TimeSpan(0, 0, 0)); _pollySyncCacheProvider.Verify(x => x.Put(It.IsAny <string>(), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Never); }
public ConnectSendGridService(IOptions <SendGridConfig> sendGridConfig, ISendGridClient sendGridClient, IMemDistCache <Template> memDistCacheTemplate, IMemDistCache <UnsubscribeGroup> memDistCacheUnsubscribeGroup) { _sendGridConfig = sendGridConfig; _sendGridClient = sendGridClient; _memDistCacheTemplate = memDistCacheTemplate; _memDistCacheUnsubscribeGroup = memDistCacheUnsubscribeGroup; }
public async Task DataNotInMemoryCache_DataNotInDistCache_DontGetData() { _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((false, null)); _distributedCacheWrapper.Setup(x => x.TryGetAsync <CachedItemWrapper <string> >(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <bool>())).ReturnsAsync((false, null)); _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0))); MemDistCacheFactory <string> memDistCacheFactory = new MemDistCacheFactory <string>(_pollySyncCacheProvider.Object, _distributedCacheWrapper.Object, _mockableDateTime.Object, _logger.Object); IMemDistCache <string> memDistCache = memDistCacheFactory.GetCache(_defaultCacheDuration, Cache.ResetTimeFactory.OnHour); string result = await memDistCache.GetCachedDataAsync(_dataGetterDelegate1, _key, RefreshBehaviour.DontWaitForFreshData, CancellationToken.None, NotInCacheBehaviour.DontGetData); Assert.IsNull(result); _pollySyncCacheProvider.Verify(x => x.TryGet(It.Is <string>(y => y == _key)), Times.Once); _distributedCacheWrapper.Verify(x => x.TryGetAsync <CachedItemWrapper <string> >(It.Is <string>(y => y == _key), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Once); await Task.Delay(_waitForBackgroundThreadToCompleteMs); // wait for background thread Assert.AreEqual(0, _numberOfTimesDataGetterDelegate1Called); DateTimeOffset whenDataWillNotBeFresh = new DateTimeOffset(2020, 05, 17, 21, 00, 00, 00, new TimeSpan(0, 0, 0)); _pollySyncCacheProvider.Verify(x => x.Put(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Never); _distributedCacheWrapper.Verify(x => x.PutAsync(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Never); }
public async Task DataNotInMemoryCache_DataInDistCacheNotFresh_WaitForFreshData() { CachedItemWrapper <string> distCacheItem = new CachedItemWrapper <string>("fromDistCache", new DateTimeOffset(2020, 05, 17, 19, 59, 59, 00, new TimeSpan(0, 0, 0))); _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((false, null)); _distributedCacheWrapper.Setup(x => x.TryGetAsync <CachedItemWrapper <string> >(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <bool>())).ReturnsAsync((true, distCacheItem)); _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0))); MemDistCacheFactory <string> memDistCacheFactory = new MemDistCacheFactory <string>(_pollySyncCacheProvider.Object, _distributedCacheWrapper.Object, _mockableDateTime.Object, _logger.Object); IMemDistCache <string> memDistCache = memDistCacheFactory.GetCache(_defaultCacheDuration, Cache.ResetTimeFactory.OnHour); string result = await memDistCache.GetCachedDataAsync(_dataGetterDelegate1, _key, RefreshBehaviour.WaitForFreshData, CancellationToken.None); Assert.AreEqual("dataFromBackendGet", result); _pollySyncCacheProvider.Verify(x => x.TryGet(It.Is <string>(y => y == _key)), Times.Once); _distributedCacheWrapper.Verify(x => x.TryGetAsync <CachedItemWrapper <string> >(It.Is <string>(y => y == _key), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Once); Assert.AreEqual(1, _numberOfTimesDataGetterDelegate1Called); DateTimeOffset whenDataWillNotBeFresh = new DateTimeOffset(2020, 05, 17, 21, 00, 00, 00, new TimeSpan(0, 0, 0)); _pollySyncCacheProvider.Verify(x => x.Put(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Once); _distributedCacheWrapper.Verify(x => x.PutAsync(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Once); }
public UserService(IUserRepository userRepository, ILogger <UserService> logger, IMemDistCache <User> memDistCache, IAddressService addressService) { _userRepository = userRepository; _logger = logger; _memDistCache = memDistCache; _addressService = addressService; }
public GroupMemberService(IGroupRepository groupRepository, IMemDistCache <List <UserGroup> > memDistCache, IGroupService groupService, IMemDistCache <UserInGroup> memDistCache_userInGroup) { _groupRepository = groupRepository; _memDistCache = memDistCache; _groupService = groupService; _memDistCache_userInGroup = memDistCache_userInGroup; }
public GroupService(IGroupRepository groupRepository, IMemDistCache <int> memDistCache_int, IMemDistCache <Group> memDistCache_group, IMemDistCache <List <Group> > memDistCache_groups, IMemDistCache <List <List <GroupCredential> > > memDistCache_listListGroupCred, IMemDistCache <Instructions> memDistCache_instructions) { _groupRepository = groupRepository; _memDistCache_int = memDistCache_int; _memDistCache_group = memDistCache_group; _memDistCache_groups = memDistCache_groups; _memDistCache_listListGroupCred = memDistCache_listListGroupCred; _memDistCache_instructions = memDistCache_instructions; }
public RequestService(IRequestHelpRepository requestHelpRepository, ILogger <RequestService> logger, IRequestHelpBuilder requestHelpBuilder, IGroupService groupService, IUserService userService, IMemDistCache <IEnumerable <JobSummary> > memDistCache, IGroupMemberService groupMemberService, IMemDistCache <IEnumerable <ShiftJob> > memDistCache_ShiftJobs, IMemDistCache <IEnumerable <RequestSummary> > memDistCache_RequestSummaries) { _requestHelpRepository = requestHelpRepository; _logger = logger; _requestHelpBuilder = requestHelpBuilder; _groupService = groupService; _userService = userService; _memDistCache = memDistCache; _groupMemberService = groupMemberService; _memDistCache_ShiftJobs = memDistCache_ShiftJobs; _memDistCache_RequestSummaries = memDistCache_RequestSummaries; _shiftJobDedupe_EqualityComparer = new JobBasicDedupe_EqualityComparer(); _jobSummaryJobDedupe_EqualityComparer = new JobBasicDedupe_EqualityComparer(); }
public async Task RefreshData() { _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0))); MemCacheFactory <string> memCacheFactory = new MemCacheFactory <string>(_pollySyncCacheProvider.Object, _mockableDateTime.Object, _logger.Object); IMemDistCache <string> memCache = memCacheFactory.GetCache(_defaultCacheDuration, ResetTimeFactory.OnHour); string result = await memCache.RefreshDataAsync(_dataGetterDelegate1, _key, CancellationToken.None); Assert.AreEqual("dataFromBackendGet", result); Assert.AreEqual(1, _numberOfTimesDataGetterDelegate1Called); DateTimeOffset whenDataWillNotBeFresh = new DateTimeOffset(2020, 05, 17, 21, 00, 00, 00, new TimeSpan(0, 0, 0)); _pollySyncCacheProvider.Verify(x => x.Put(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Once); }
public AddressService( ILogger <AddressService> logger, IConfiguration configuration, IAddressRepository addressRepository, IUserRepository userRepository, IMemDistCache <LocationDetails> memDistCache, IMemDistCache <IEnumerable <LocationDetails> > memDistCache_LocationDetailsList, IMemDistCache <IEnumerable <LocationWithDistance> > memDistCache_LocationDistanceList, IMemDistCache <double> memDistCache_PostcodeDistances, IGroupRepository groupRepository, IGroupMemberService groupMemberService, HttpClient client) : base(client, configuration, "Services:Address") { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _addressRepository = addressRepository; _userRepository = userRepository; _groupRepository = groupRepository; _groupMemberService = groupMemberService; _memDistCache = memDistCache; _memDistCache_LocationDetailsList = memDistCache_LocationDetailsList; _memDistCache_LocationDistanceList = memDistCache_LocationDistanceList; _memDistCache_PostcodeDistances = memDistCache_PostcodeDistances; }
public async Task DataInMemoryCacheAndFresh() { CachedItemWrapper <string> memoryCacheItem = new CachedItemWrapper <string>("fromMemDistCache", new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0))); _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((true, memoryCacheItem)); _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0))); MemDistCacheFactory <string> memDistCacheFactory = new MemDistCacheFactory <string>(_pollySyncCacheProvider.Object, _distributedCacheWrapper.Object, _mockableDateTime.Object, _logger.Object); IMemDistCache <string> memDistCache = memDistCacheFactory.GetCache(_defaultCacheDuration, Cache.ResetTimeFactory.OnHour); string result = await memDistCache.GetCachedDataAsync(_dataGetterDelegate1, _key, RefreshBehaviour.DontWaitForFreshData, CancellationToken.None); Assert.AreEqual("fromMemDistCache", result); Assert.AreEqual(0, _numberOfTimesDataGetterDelegate1Called); _pollySyncCacheProvider.Verify(x => x.TryGet(It.Is <string>(y => y == _key)), Times.Once); _pollySyncCacheProvider.Verify(x => x.Put(It.IsAny <string>(), It.IsAny <CachedItemWrapper <string> >(), It.IsAny <Ttl>()), Times.Never); _distributedCacheWrapper.Verify(x => x.TryGetAsync <string>(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Never); _distributedCacheWrapper.Verify(x => x.PutAsync(It.IsAny <string>(), It.IsAny <CachedItemWrapper <string> >(), It.IsAny <Ttl>(), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Never); }
public async Task MultipleConcurrentRequests() { int numberOfTimesDataGetterDelegate1Called = 0; Func <CancellationToken, Task <string> > dataGetterDelegate1 = async(token) => { Interlocked.Increment(ref numberOfTimesDataGetterDelegate1Called); await Task.Delay(1000); return(await Task.FromResult("dataFromBackendGet1")); }; int numberOfTimesDataGetterDelegate2Called = 0; Func <CancellationToken, Task <string> > dataGetterDelegate2 = async(token) => { Interlocked.Increment(ref numberOfTimesDataGetterDelegate2Called); await Task.Delay(1000); return(await Task.FromResult("dataFromBackendGet2")); }; _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((false, null)); _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0))); MemCacheFactory <string> memCacheFactory = new MemCacheFactory <string>(_pollySyncCacheProvider.Object, _mockableDateTime.Object, _logger.Object); IMemDistCache <string> memCache = memCacheFactory.GetCache(_defaultCacheDuration, ResetTimeFactory.OnHour); ConcurrentBag <Task <string> > results1 = new ConcurrentBag <Task <string> >(); ConcurrentBag <Task <string> > results2 = new ConcurrentBag <Task <string> >(); Stopwatch stopwatch = Stopwatch.StartNew(); Task task1 = Task.Factory.StartNew(() => { Parallel.For(0, 50, i => { Task <string> result = memCache.GetCachedDataAsync(dataGetterDelegate1, "key1", RefreshBehaviour.WaitForFreshData, CancellationToken.None); results1.Add(result); }); }); Task task2 = Task.Factory.StartNew(() => { Parallel.For(0, 50, i => { Task <string> result = memCache.GetCachedDataAsync(dataGetterDelegate2, "key2", RefreshBehaviour.WaitForFreshData, CancellationToken.None); results2.Add(result); }); }); Task.WaitAll(task1, task2); await Task.WhenAll(results1); await Task.WhenAll(results2); stopwatch.Stop(); Assert.Less(stopwatch.ElapsedMilliseconds, 2000); // shows the calls were processed concurrently foreach (Task <string> result in results1) { Assert.AreEqual("dataFromBackendGet1", await result); } foreach (Task <string> result in results2) { Assert.AreEqual("dataFromBackendGet2", await result); } Assert.AreEqual(1, numberOfTimesDataGetterDelegate1Called); Assert.AreEqual(1, numberOfTimesDataGetterDelegate2Called); await Task.Delay(_waitForBackgroundThreadToCompleteMs); // wait for background thread DateTimeOffset whenDataWillNotBeFresh = new DateTimeOffset(2020, 05, 17, 21, 00, 00, 00, new TimeSpan(0, 0, 0)); _pollySyncCacheProvider.Verify(x => x.Put(It.IsAny <string>(), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet1" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Once); _pollySyncCacheProvider.Verify(x => x.Put(It.IsAny <string>(), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet2" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Once); }
public GetVolunteerCoordinatesHandler(IMemDistCache <IEnumerable <CachedVolunteerDto> > memDistCache, IVolunteerCache volunteerCache, IVolunteersFilteredByMinDistanceGetter volunteersFilteredByMinDistanceGetter) { _memDistCache = memDistCache; _volunteerCache = volunteerCache; _volunteersFilteredByMinDistanceGetter = volunteersFilteredByMinDistanceGetter; }
public VolunteerCache(IMemDistCache <IEnumerable <CachedVolunteerDto> > memDistCache, IVolunteersForCacheGetter volunteersForCacheGetter) { _memDistCache = memDistCache; _volunteersForCacheGetter = volunteersForCacheGetter; }
public GroupService(IHttpClientWrapper httpClientWrapper, IMemDistCache <double?> memDistCache) { _httpClientWrapper = httpClientWrapper; _memDistCache = memDistCache; }
public ConnectAddressService(IHttpClientWrapper httpClientWrapper, IMemDistCache <LocationDetails> memDistCache) { _httpClientWrapper = httpClientWrapper; _memDistCache = memDistCache; }
public TimedCacheRefresher(IMemDistCache <IEnumerable <CachedVolunteerDto> > memDistCache, IVolunteersForCacheGetter volunteersForCacheGetter, ILoggerWrapper <TimedCacheRefresher> logger) { _memDistCache = memDistCache; _volunteersForCacheGetter = volunteersForCacheGetter; _logger = logger; }