Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit 90a0731

Browse files
committed
Handle cache unreliability #99
1 parent bde79d6 commit 90a0731

File tree

2 files changed

+89
-17
lines changed

2 files changed

+89
-17
lines changed

src/Microsoft.AspNetCore.Session/DistributedSession.cs

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public string Id
7575
{
7676
get
7777
{
78-
Load(); // TODO: Silent failure
78+
Load();
7979
if (_sessionId == null)
8080
{
8181
_sessionId = new Guid(IdBytes).ToString();
@@ -88,7 +88,7 @@ private byte[] IdBytes
8888
{
8989
get
9090
{
91-
Load(); // TODO: Silent failure
91+
Load();
9292
if (_sessionIdBytes == null)
9393
{
9494
_sessionIdBytes = new byte[IdByteCount];
@@ -102,14 +102,14 @@ public IEnumerable<string> Keys
102102
{
103103
get
104104
{
105-
Load(); // TODO: Silent failure
105+
Load();
106106
return _store.Keys.Select(key => key.KeyString);
107107
}
108108
}
109109

110110
public bool TryGetValue(string key, out byte[] value)
111111
{
112-
Load(); // TODO: Silent failure
112+
Load();
113113
return _store.TryGetValue(new EncodedKey(key), out value);
114114
}
115115

@@ -155,7 +155,17 @@ private void Load()
155155
{
156156
if (!_loaded)
157157
{
158-
var data = _cache.Get(_sessionKey);
158+
byte[] data = null;
159+
try
160+
{
161+
data = _cache.Get(_sessionKey);
162+
}
163+
catch (Exception exception)
164+
{
165+
_logger.SessionCacheReadException(_sessionKey, exception);
166+
return;
167+
}
168+
159169
if (data != null)
160170
{
161171
Deserialize(new MemoryStream(data));
@@ -174,7 +184,17 @@ public async Task LoadAsync()
174184
{
175185
if (!_loaded)
176186
{
177-
var data = await _cache.GetAsync(_sessionKey);
187+
byte[] data = null;
188+
try
189+
{
190+
data = await _cache.GetAsync(_sessionKey);
191+
}
192+
catch (Exception exception)
193+
{
194+
_logger.SessionCacheReadException(_sessionKey, exception);
195+
return;
196+
}
197+
178198
if (data != null)
179199
{
180200
Deserialize(new MemoryStream(data));
@@ -191,28 +211,50 @@ public async Task CommitAsync()
191211
{
192212
if (_isModified)
193213
{
194-
var data = await _cache.GetAsync(_sessionKey);
195-
if (_logger.IsEnabled(LogLevel.Information) && data == null)
214+
byte[] data = null;
215+
try
216+
{
217+
data = await _cache.GetAsync(_sessionKey);
218+
}
219+
catch (Exception exception)
220+
{
221+
_logger.SessionCacheReadException(_sessionKey, exception);
222+
return;
223+
}
224+
225+
if (data == null)
196226
{
197227
_logger.SessionStarted(_sessionKey, Id);
198228
}
199229
_isModified = false;
200230

201231
var stream = new MemoryStream();
202232
Serialize(stream);
203-
await _cache.SetAsync(
204-
_sessionKey,
205-
stream.ToArray(),
206-
new DistributedCacheEntryOptions().SetSlidingExpiration(_idleTimeout));
207233

208-
if (_logger.IsEnabled(LogLevel.Debug))
234+
try
209235
{
210-
_logger.SessionStored(_sessionKey, Id, _store.Count);
236+
await _cache.SetAsync(
237+
_sessionKey,
238+
stream.ToArray(),
239+
new DistributedCacheEntryOptions().SetSlidingExpiration(_idleTimeout));
211240
}
241+
catch (Exception exception)
242+
{
243+
_logger.SessionCacheWriteException(_sessionKey, Id, exception);
244+
return;
245+
}
246+
_logger.SessionStored(_sessionKey, Id, _store.Count);
212247
}
213248
else
214249
{
215-
await _cache.RefreshAsync(_sessionKey);
250+
try
251+
{
252+
await _cache.RefreshAsync(_sessionKey);
253+
}
254+
catch (Exception exception)
255+
{
256+
_logger.SessionCacheRefreshException(_sessionKey, exception);
257+
}
216258
}
217259
}
218260

src/Microsoft.AspNetCore.Session/LoggingExtensions.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ internal static class LoggingExtensions
1212
private static Action<ILogger, string, string, Exception> _sessionStarted;
1313
private static Action<ILogger, string, string, int, Exception> _sessionLoaded;
1414
private static Action<ILogger, string, string, int, Exception> _sessionStored;
15+
private static Action<ILogger, string, Exception> _sessionCacheReadException;
16+
private static Action<ILogger, string, string, Exception> _sessionCacheWriteException;
17+
private static Action<ILogger, string, Exception> _sessionCacheRefreshException;
1518
private static Action<ILogger, Exception> _errorUnprotectingCookie;
1619

1720
static LoggingExtensions()
@@ -23,7 +26,7 @@ static LoggingExtensions()
2326
_accessingExpiredSession = LoggerMessage.Define<string>(
2427
eventId: 2,
2528
logLevel: LogLevel.Warning,
26-
formatString: "Accessing expired session; Key:{sessionKey}");
29+
formatString: "Accessing expired session, Key:{sessionKey}");
2730
_sessionStarted = LoggerMessage.Define<string, string>(
2831
eventId: 3,
2932
logLevel: LogLevel.Information,
@@ -36,8 +39,20 @@ static LoggingExtensions()
3639
eventId: 5,
3740
logLevel: LogLevel.Debug,
3841
formatString: "Session stored; Key:{sessionKey}, Id:{sessionId}, Count:{count}");
39-
_errorUnprotectingCookie = LoggerMessage.Define(
42+
_sessionCacheReadException = LoggerMessage.Define<string>(
4043
eventId: 6,
44+
logLevel: LogLevel.Error,
45+
formatString: "Session read exceptionm, Key:{sessionKey}, Exception:{exception}");
46+
_sessionCacheWriteException = LoggerMessage.Define<string, string>(
47+
eventId: 7,
48+
logLevel: LogLevel.Error,
49+
formatString: "Session write exception, Key:{sessionKey}, Id:{sessionId}, Exception:{exception}");
50+
_sessionCacheRefreshException = LoggerMessage.Define<string>(
51+
eventId: 8,
52+
logLevel: LogLevel.Error,
53+
formatString: "Session refresh exception, Key:{sessionKey}, Exception:{exception}");
54+
_errorUnprotectingCookie = LoggerMessage.Define(
55+
eventId: 9,
4156
logLevel: LogLevel.Warning,
4257
formatString: "Error unprotecting the session cookie.");
4358
}
@@ -67,6 +82,21 @@ public static void SessionStored(this ILogger logger, string sessionKey, string
6782
_sessionStored(logger, sessionKey, sessionId, count, null);
6883
}
6984

85+
public static void SessionCacheReadException(this ILogger logger, string sessionKey, Exception exception)
86+
{
87+
_sessionCacheReadException(logger, sessionKey, exception);
88+
}
89+
90+
public static void SessionCacheWriteException(this ILogger logger, string sessionKey, string sessionId, Exception exception)
91+
{
92+
_sessionCacheWriteException(logger, sessionKey, sessionId, exception);
93+
}
94+
95+
public static void SessionCacheRefreshException(this ILogger logger, string sessionKey, Exception exception)
96+
{
97+
_sessionCacheRefreshException(logger, sessionKey, exception);
98+
}
99+
70100
public static void ErrorUnprotectingSessionCookie(this ILogger logger, Exception exception)
71101
{
72102
_errorUnprotectingCookie(logger, exception);

0 commit comments

Comments
 (0)