Esempio 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());
        }
Esempio n. 2
0
        public async Task <int> GetUserId()
        {
            if (_appCache.Contains(UserIdKey))
            {
                return(_appCache.Get <int>(UserIdKey));
            }

            int userId;
            var user = await _userService.GetByUserNameAsync(UserName);

            if (user == null)
            {
                // No user exists so let's create one
                user = new Data.Models.User
                {
                    Name     = UserName,
                    UserName = UserName
                };
                userId = await _userService.CreateAsync(user /*, _appConfigHelper.SystemUserId*/);
            }
            else
            {
                userId = user.Id;
            }

            _appCache.Add(UserIdKey, userId);

            return(userId);
        }