コード例 #1
0
        public void GetDefaultCacheOptions_HasCorrectExpireTime()
        {
            // Arrange
            const int expecteSeconds = 3600;

            // Act
            var options  = CacheConstants.GetDefaultCacheOptions();
            var lifeSpan = options?.SlidingExpiration?.TotalSeconds;

            // Assert
            Assert.NotNull(lifeSpan);
            Assert.Equal(expecteSeconds, lifeSpan);
        }
コード例 #2
0
        /// <inheritdoc />
        public async Task <IEnumerable <ApplicationUserDto> > GetAllUsersOrderedByNameAsync()
        {
            const string cacheKey = CacheConstants.AllUsersCacheKey;

            if (!_cache.TryGetValue(cacheKey, out List <ApplicationUserDto> users))
            {
                users = await _userManager
                        .Users
                        .Select(u => new ApplicationUserDto(u))
                        .OrderBy(u => u.Name)
                        .ToListAsync();

                _cache.Set(cacheKey, users, CacheConstants.GetDefaultCacheOptions());
            }
            return(users);
        }
コード例 #3
0
        /// <inheritdoc />
        /// <exception cref="UserNotFoundException">Thrown when user is not found</exception>
        public async Task <ApplicationUserDto> GetUserByIdAsync(string userId)
        {
            var cacheKey = CacheConstants.GetSingleUserCacheKey(userId);

            if (!_cache.TryGetValue(cacheKey, out ApplicationUser user))
            {
                user = await _userManager.FindByIdAsync(userId);

                if (user == null)
                {
                    throw new UserNotFoundException();
                }
                _cache.Set(cacheKey, user, CacheConstants.GetDefaultCacheOptions());
            }
            return(new ApplicationUserDto(user));
        }
コード例 #4
0
        /// <inheritdoc />
        /// <exception cref="TodoNotFoundException">When todo is not found</exception>
        public async Task <TodoDto> GetTodoByIdAsync(int todoId, string userId)
        {
            var cacheKey = CacheConstants.GetSingleTodoCacheKey(todoId, userId);

            if (!_cache.TryGetValue(cacheKey, out Todo todo))
            {
                todo = await _db.Todo.SingleOrDefaultAsync(t =>
                                                           t.Id == todoId && t.Owner.Id == userId);

                if (todo == null)
                {
                    throw new TodoNotFoundException();
                }
                _cache.Set(cacheKey, todo, CacheConstants.GetDefaultCacheOptions());
            }
            return(new TodoDto(todo));
        }
コード例 #5
0
        /// <summary>
        /// Filters the list by a date (ignoring time).
        /// </summary>
        /// <param name="date">Valid date to filter by</param>
        /// <param name="userId">The id of the token owner</param>
        /// <returns>List of todos</returns>
        private async Task <IEnumerable <TodoDto> > GetAllTodosForDayOrderedByDueAsync(DateTime date, string userId)
        {
            var cacheKey = CacheConstants.GetAllTodosForDayCacheKey(userId, date);

            if (!_cache.TryGetValue(cacheKey, out List <TodoDto> todos))
            {
                todos = await(from t in _db.Todo
                              where t.Due.Date == date && t.Owner.Id == userId
                              orderby t.Due
                              select new TodoDto(t)).ToListAsync();
                if (todos.Any())
                {
                    _cache.Set(cacheKey, CacheConstants.GetDefaultCacheOptions());
                }
            }
            return(todos);
        }
コード例 #6
0
        /// <inheritdoc />
        /// <exception cref="UserNotFoundException">When todo is not found</exception>
        public async Task <int> CreateTodoAsync(CreateTodoViewModel todo, string userId)
        {
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                throw new UserNotFoundException();
            }
            var newTodo = new Todo(todo, user);
            await _db.AddAsync(newTodo);

            await _db.SaveChangesAsync();

            // Add to single cache and clear list cache
            _cache.Set(CacheConstants.GetSingleTodoCacheKey(newTodo.Id, userId),
                       newTodo, CacheConstants.GetDefaultCacheOptions());
            _cache.Remove(CacheConstants.GetAllTodosCacheKey(userId));
            _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, newTodo.Due));

            return(newTodo.Id);
        }
コード例 #7
0
        /// <inheritdoc />
        /// <exception cref="TodoNotFoundException">When todo is not found</exception>
        public async Task EditTodoAsync(EditTodoViewModel model, string userId)
        {
            var todo = await _db.Todo.SingleOrDefaultAsync(t => t.Id == model.Id && t.Owner.Id == userId);

            if (todo == null)
            {
                throw new TodoNotFoundException();
            }
            var oldDate = todo.Due;

            todo.Edit(model);
            await _db.SaveChangesAsync();

            // Set cache for single and clear related lists
            _cache.Set(CacheConstants.GetSingleTodoCacheKey(todo.Id, userId),
                       todo, CacheConstants.GetDefaultCacheOptions());
            _cache.Remove(CacheConstants.GetAllTodosCacheKey(userId));
            _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, oldDate));
            if (oldDate.Date != todo.Due.Date)
            {
                _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, todo.Due));
            }
        }