private SessionData CheckForNewer(string sessionId, SessionData data) { string cacheKey = _sessionKeyFormat.GetPrimaryKey(sessionId); // try if there is fresher copy // This happens when a node goes up or down if (AssignPrimaryBackupNodes(cacheKey)) { var newData = _client.Get<SessionData>(cacheKey); if (newData == null || data.SavedAt > newData.SavedAt) { // If not found or older, that means this client hit the key first // so relocate session for next call Store(sessionId, data, TimeSpan.FromMinutes(data.Timeout)); } else { // else found newer, that means some other client already updated this data = newData; } } return data; }
public void Store(string sessionId, SessionData cacheItem, TimeSpan timeout) { var cacheKey = _sessionKeyFormat.GetPrimaryKey(sessionId); AssignPrimaryBackupNodes(cacheKey); cacheItem.SavedAt = DateTime.UtcNow.Ticks; _client.Store(StoreMode.Set, cacheKey, cacheItem, timeout); if (IsBackupEnabled()) // backup { var backupKey = _sessionKeyFormat.GetBackupKey(sessionId); _client.Store(StoreMode.Set, backupKey, cacheItem, timeout); } }