/// <summary> /// Gets or create a user session if one doesn't exist. /// </summary> /// <param name="userId">The user global id.</param> /// <param name="userName">Title of the user.</param> /// <param name="shardId"></param> /// <returns></returns> public async ValueTask <UserSessionAsync> GetOrCreateSessionAsync(Guid userId, string userName, string shardId) { var userSession = await this.GetUserSessionAsync(userId); if (userSession == null) { userSession = new UserSessionAsync(userId, userName, shardId); await this.cacheClient.AddAsync(userSession.ToCacheKey(), userSession, userSession.ExpiryDate.GetValueOrDefault(DateTime.UtcNow) + TimeSpan.FromHours(1)); } return(userSession); }
/// <summary> /// Updates the UserSession in the cache, or removes expired ones. /// </summary> /// <param name="userSession">The user session.</param> public async ValueTask UpdateUserSessionAsync(UserSessionAsync userSession) { var hasSessionExpired = userSession.HasExpired(); if (hasSessionExpired) { LogIfDebug("Session has expired, removing: " + userSession.ToCacheKey()); await this.cacheClient.RemoveAsync(userSession.ToCacheKey()); } else { LogIfDebug("Updating session: " + userSession.ToCacheKey()); await this.cacheClient.ReplaceAsync(userSession.ToCacheKey(), userSession, userSession.ExpiryDate.Value); } }