Exemplo n.º 1
0
        public virtual async Task <List <T> > GetAllWhereAsync(Func <T, bool> filterPredicate, bool skipDatabaseConsistencyCheck = false)
        {
            if (filterPredicate == null)
            {
                return(new List <T>());
            }

            // If this is the first time the GetAllAsync has been called, we return 0, despite the fact that there might be some in the cache.
            // This is because we want to force the call to the database to ensure that we have a true "all" which will also then populate the cache
            // in the service layer.
            if (skipDatabaseConsistencyCheck)
            {
                var allItemsNoDbCheck = await _appCache.GetAllItemsInPartitionAsync <T>(PartitionNameFormatter()).ConfigureAwait(false);

                return(allItemsNoDbCheck.Where(filterPredicate).ToList());
            }
            var key         = ComposeLastCallToGetAllCacheKey();
            var containsKey = _appCache.Contains(key);

            if (!containsKey)
            {
                _appCache.AddOrUpdate(key, DateTimeOffset.UtcNow, TimeSpan.FromDays(2));
                return(new List <T>());
            }
            _appCache.AddOrUpdate(key, DateTimeOffset.UtcNow, TimeSpan.FromDays(2));
            var allItems = await _appCache.GetAllItemsInPartitionAsync <T>(PartitionNameFormatter()).ConfigureAwait(false);

            return(allItems.Where(filterPredicate).ToList());
        }