public async Task <LoggedInUser> GetLoggedInUserAsync(Guid loggedInUserId) { var cacheKey = WebCacheKey.LoggedInUser(loggedInUserId); var expiry = TimeSpan.FromHours(1); var loggedInUser = await _cache.GetWithSlidingExpirationAsync <LoggedInUser>(cacheKey, expiry); if (loggedInUser != null) { return(loggedInUser); } try { var commonUser = await _commonCtx.Users.SingleOrDefaultAsync(u => u.Id == loggedInUserId); if (commonUser == null) { _logger.Error($"No matching {nameof(CommonUser)} record for loggedInUserId={loggedInUserId}"); return(null); } loggedInUser = Mapper.Map <LoggedInUser>(commonUser); await _cache.SetAsync(cacheKey, loggedInUser, expiry); } catch (Exception ex) { } return(loggedInUser); }
public async Task <IActionResult> PurgeCache() { var accountUsers = await CommonContext.UserAccounts.Include(m => m.Account).AsNoTracking().ToListAsync(); var cachKeys = new List <string>(); var accounts = accountUsers.Select(m => m.Account).Distinct(); //CommonAccounts foreach (var account in accounts) { cachKeys.Add(WebCacheKey.CommonAccount(account.Number)); } cachKeys.Add(WebCacheKey.CommonAccounts); //Account Users foreach (var accountUser in accountUsers) { cachKeys.Add(WebCacheKey.CommonUserAccount(accountUser.Account.Number, accountUser.UserId.Value)); } var users = await CommonContext.Users.Select(m => m.Id).ToListAsync(); foreach (var user in users) { cachKeys.Add(WebCacheKey.LoggedInUser(user)); } cachKeys.Add(WebCacheKey.HealthCheck); cachKeys.Add(WebCacheKey.CommonViolationTypes); cachKeys.Add(WebCacheKey.CommonViolationCategories); cachKeys.Add(WebCacheKey.CommonViolations); cachKeys.Add(WebCacheKey.Violations); await _cache.RemoveAsync(cachKeys.ToArray()); return(RedirectToAction("Index")); }
protected async Task PurgeLoggedInUser(Guid id) { var cacheKey = WebCacheKey.LoggedInUser(id); await _cache.RemoveAsync(cacheKey); }
public async Task <IActionResult> UpdateProfile([FromBody] UserModel model) { var response = new APIResponse <UserModel>(); if (ModelState.IsValid) { // if user is trying to update someone else profile. if (User.GetJWTLoggedInUserId() != model.Id) { response.Success = false; response.Errors.Add(new Error { Code = 0, Message = "Invalid ID" }); return(Ok(response)); } var userAlreadyExists = await CommonContext.Users.AsNoTracking().AnyAsync(q => q.Email.ToLower() == model.Email.ToLower() && q.Id != model.Id); if (userAlreadyExists) { // This isn't a security risk because we've verified the email address already response.Success = false; response.Errors.Add(new Error { Code = 0, Message = "A user has already verified that email address." }); return(Ok(response)); } var user = await CommonContext.Users.SingleAsync(m => m.Id == model.Id); user.Email = model.Email; user.FirstName = model.FirstName; user.LastName = model.LastName; user.PhoneNumber = model.PhoneNumber; user.ProfileImageKey = model.profileImageKey; if (!string.IsNullOrWhiteSpace(model.Password)) { user.SetPassword(model.Password); } CommonContext.Users.Update(user); await CommonContext.SaveChangesAsync(); //Update user information in all other accounts await _commonAccountSvc.SaveAccountUser(user, model.Id); //purge the user cache var cacheKey = WebCacheKey.LoggedInUser(model.Id); await _cache.RemoveAsync(cacheKey); response.Message = "Your profile has been updated"; } else { response.Success = false; response.Errors.AddRange(ModelState.ToErrors()); } return(Ok(response)); }