Пример #1
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);
        }
Пример #2
0
        /// <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);