public async Task SetSessionItem(UserSessionItem sessionKeyItem, CancellationToken cancellationToken) { var sessionKeyItems = await StateManager.GetOrAddAsync <IReliableDictionary <UserSessionItemId, UserSessionItem> >(SessionKeyDictionaryName); ServiceEventSource.Current.Message($"Method started {nameof(UserSessionService)}->SetSessionItem() at: {DateTime.UtcNow}"); using (var tx = StateManager.CreateTransaction()) { await sessionKeyItems.AddOrUpdateAsync(tx, sessionKeyItem.Id, sessionKeyItem, (key, value) => sessionKeyItem); await tx.CommitAsync(); } ServiceEventSource.Current.Message($"Method ended {nameof(UserSessionService)}->SetSessionItem() at: {DateTime.UtcNow}"); }
public async Task RemoveSessionItem(string userSessionId, UserSessionItem userSessionItem) { try { // // In fact we are doing here a request to get a needed service based on partion key generated from userSessionId and // on next step we will need to get an item with userSessionId, this looks as not nessesary query but we have a different service for the differnt data ( sharding ) // var userSessionService = GetSessionServiceByUserSessionId(userSessionId); await userSessionService?.RemoveSessionItem(userSessionItem.Id, CancellationToken.None); } catch (Exception ex) { ServiceEventSource.Current.Message($"Method {nameof(SessionService)}->RemoveSessionItem<T>() failed with error: {ex.ToString()} at: {DateTime.UtcNow}"); } }
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken)) { var sessionItem = await SessionService.GetSessionItem(key, key); if (sessionItem == null) { sessionItem = new UserSessionItem(key, value, new UserSessionItemId(key)); } sessionItem.CreateDate = DateTime.UtcNow; ExpiryType expiryType; var epoctime = GetEpochExpiry(options, out expiryType); sessionItem.Epoctime = epoctime; sessionItem.ExpiryType = expiryType.ToString(); sessionItem.Ttl = DateTimeOffset.Now.ToUniversalTime().ToUnixTimeSeconds() + (_sessionMinutes * 60); await SessionService.AddSessionItem(key, sessionItem); }
public async Task <UserSessionItem> GetSessionItem(UserSessionItemId sessionKeyItemId, CancellationToken cancellationToken) { var sessionKeyItems = await StateManager.GetOrAddAsync <IReliableDictionary <UserSessionItemId, UserSessionItem> >(SessionKeyDictionaryName); ServiceEventSource.Current.Message($"Method started {nameof(UserSessionService)}->GetSessionItem() at: {DateTime.UtcNow}"); UserSessionItem result = null; using (var tx = StateManager.CreateTransaction()) { var enumerator = (await sessionKeyItems.CreateEnumerableAsync(tx)).GetAsyncEnumerator(); while (await enumerator.MoveNextAsync(cancellationToken)) { if (enumerator.Current.Key == sessionKeyItemId) { result = enumerator.Current.Value; break; } } } ServiceEventSource.Current.Message($"Method ended {nameof(UserSessionService)}->GetSessionItem() at: {DateTime.UtcNow}"); return(result); }