示例#1
0
        public async Task <IEnumerable <ShiftJob> > GetOpenShiftsForUserAsync(User user, DateTime?dateFrom, DateTime?dateTo, bool waitForData, CancellationToken cancellationToken)
        {
            NotInCacheBehaviour notInCacheBehaviour = waitForData ? NotInCacheBehaviour.WaitForData : NotInCacheBehaviour.DontWaitForData;

            return(await _memDistCache_ShiftJobs.GetCachedDataAsync(async (cancellationToken) =>
            {
                return await GetOpenShiftsForUserFromRepo(user, dateFrom, dateTo, cancellationToken);
            }, $"{CACHE_KEY_PREFIX}-user-{user.ID}-open-shifts-from-{dateFrom}-to-{dateTo}", RefreshBehaviour.DontWaitForFreshData, cancellationToken, notInCacheBehaviour));
        }
示例#2
0
        public async Task <IEnumerable <JobSummary> > GetOpenJobsAsync(User user, bool waitForData, CancellationToken cancellationToken)
        {
            RefreshBehaviour    refreshBehaviour    = waitForData ? RefreshBehaviour.WaitForFreshData : RefreshBehaviour.DontWaitForFreshData;
            NotInCacheBehaviour notInCacheBehaviour = waitForData ? NotInCacheBehaviour.WaitForData : NotInCacheBehaviour.DontWaitForData;

            var jobs = await _memDistCache.GetCachedDataAsync(async (cancellationToken) =>
            {
                return(await GetOpenJobsForUserFromRepo(user, cancellationToken));
            }, $"{CACHE_KEY_PREFIX}-user-{user.ID}-open-jobs", refreshBehaviour, cancellationToken, notInCacheBehaviour);

            return(jobs);
        }
示例#3
0
        public async Task <IEnumerable <JobSummary> > GetJobsForUserAsync(int userId, bool waitForData, CancellationToken cancellationToken)
        {
            NotInCacheBehaviour notInCacheBehaviour = waitForData ? NotInCacheBehaviour.WaitForData : NotInCacheBehaviour.DontWaitForData;

            var jobs = await _memDistCache.GetCachedDataAsync(async (cancellationToken) =>
            {
                var request = new GetAllJobsByFilterRequest {
                    AllocatedToUserId = userId
                };
                var response = await _requestHelpRepository.GetAllJobsByFilterAsync(request);
                return(response.JobSummaries);
            }, $"{CACHE_KEY_PREFIX}-user-{userId}-accepted-jobs", RefreshBehaviour.DontWaitForFreshData, cancellationToken, notInCacheBehaviour);

            if (jobs != null)
            {
                return(jobs.OrderOpenJobsForDisplay());
            }
            throw new Exception($"Unable to get jobs for user {userId}");
        }
示例#4
0
        public async Task <IEnumerable <ShiftJob> > GetShiftsForUserAsync(int userId, DateTime?dateFrom, DateTime?dateTo, bool waitForData, CancellationToken cancellationToken)
        {
            NotInCacheBehaviour notInCacheBehaviour = waitForData ? NotInCacheBehaviour.WaitForData : NotInCacheBehaviour.DontWaitForData;

            return(await _memDistCache_ShiftJobs.GetCachedDataAsync(async (cancellationToken) =>
            {
                return await _requestHelpRepository.GetUserShiftJobsByFilter(new GetUserShiftJobsByFilterRequest()
                {
                    VolunteerUserId = userId,
                    DateFrom = dateFrom,
                    DateTo = dateTo,
                    JobStatusRequest = new JobStatusRequest()
                    {
                        JobStatuses = new List <JobStatuses>()
                        {
                            JobStatuses.Accepted, JobStatuses.InProgress, JobStatuses.Done
                        }
                    }
                });
            }, $"{CACHE_KEY_PREFIX}-user-{userId}-user-shifts-from-{dateFrom}-to-{dateTo}", RefreshBehaviour.DontWaitForFreshData, cancellationToken, notInCacheBehaviour));
        }
        /// <inheritdoc />>
        public async Task <T> GetCachedDataAsync(Func <CancellationToken, Task <T> > dataGetter, string key, RefreshBehaviour refreshBehaviour, CancellationToken cancellationToken, NotInCacheBehaviour notInCacheBehaviour)
        {
            (bool, object)memoryWrappedResult = _pollySyncCacheProvider.TryGet(key);

            bool isObjectInMemoryCache = memoryWrappedResult.Item1;

            if (isObjectInMemoryCache)
            {
                CachedItemWrapper <T> memoryResultObject = (CachedItemWrapper <T>)memoryWrappedResult.Item2;
                bool isMemoryCacheFresh = IsFresh(memoryResultObject);

                if (!isMemoryCacheFresh)
                {
                    if (refreshBehaviour == RefreshBehaviour.WaitForFreshData)
                    {
                        return(await _collapserPolicy.ExecuteAsync(async() => await RecacheItemInMemoryAndDistCacheAsync(dataGetter, key, cancellationToken, _whenDataIsStaleDelegate)));
                    }
                    else if (refreshBehaviour == RefreshBehaviour.DontWaitForFreshData)
                    {
#pragma warning disable 4014
                        Task.Factory.StartNew(async() => await RecacheItemInMemoryAndDistCacheAsync(dataGetter, key, cancellationToken, _whenDataIsStaleDelegate), cancellationToken);
#pragma warning restore 4014
                    }
                }

                return(memoryResultObject.Content);
            }
            else
            {
                (bool, object)distributedCacheWrappedResult = await _distributedCacheWrapper.TryGetAsync <CachedItemWrapper <T> >(key, cancellationToken, false);

                bool isObjectInDistributedCache = distributedCacheWrappedResult.Item1;

                if (isObjectInDistributedCache)
                {
                    CachedItemWrapper <T> distributedCacheObject = (CachedItemWrapper <T>)distributedCacheWrappedResult.Item2;
                    bool isDistributedCacheFresh = IsFresh(distributedCacheObject);

                    if (isDistributedCacheFresh)
                    {
                        AddToMemoryCache(key, distributedCacheObject);
                    }
                    else
                    {
                        if (refreshBehaviour == RefreshBehaviour.WaitForFreshData)
                        {
                            return(await RecacheItemInMemoryAndDistCacheAsync(dataGetter, key, cancellationToken, _whenDataIsStaleDelegate));
                        }
                        else if (refreshBehaviour == RefreshBehaviour.DontWaitForFreshData)
                        {
#pragma warning disable 4014
                            Task.Factory.StartNew(async() => await RecacheItemInMemoryAndDistCacheAsync(dataGetter, key, cancellationToken, _whenDataIsStaleDelegate), cancellationToken);
#pragma warning restore 4014
                        }
                    }

                    return(distributedCacheObject.Content);
                }
            }

            // data isn't in either memory or distributed cache
            if (notInCacheBehaviour == NotInCacheBehaviour.WaitForData)
            {
                return(await RecacheItemInMemoryAndDistCacheAsync(dataGetter, key, cancellationToken, _whenDataIsStaleDelegate));
            }
            else if (notInCacheBehaviour == NotInCacheBehaviour.DontWaitForData)
            {
#pragma warning disable 4014
                Task.Factory.StartNew(async() => await RecacheItemInMemoryAndDistCacheAsync(dataGetter, key, cancellationToken, _whenDataIsStaleDelegate), cancellationToken);
#pragma warning restore 4014
            }

            return(default);