/// <summary> /// Gets the user by the id, either from the cache or the database. /// </summary> /// <param name="userId">The id of the user to get.</param> /// <returns>Returns the user info, either from the cache or the database if found, otherwise null.</returns> public async Task <User> GetByIdAsync(int userId) { // Gets the user from cache: User user = _userCache.Get(userId); if (user == null) { // User not found in cache - so gets from the database: user = await GetUserByIdFromDatabase(userId); if (user == null) { throw new Exception("The user id was not found in the database."); } _userCache.Set(user); } return(user); }