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

Commit db918af

Browse files
committed
Handle cache unreliability #99
1 parent 484801a commit db918af

File tree

2 files changed

+88
-16
lines changed

2 files changed

+88
-16
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: 31 additions & 1 deletion
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

1619
static LoggingExtensions()
1720
{
@@ -22,7 +25,7 @@ static LoggingExtensions()
2225
_accessingExpiredSession = LoggerMessage.Define<string>(
2326
eventId: 2,
2427
logLevel: LogLevel.Warning,
25-
formatString: "Accessing expired session; Key:{sessionKey}");
28+
formatString: "Accessing expired session, Key:{sessionKey}");
2629
_sessionStarted = LoggerMessage.Define<string, string>(
2730
eventId: 3,
2831
logLevel: LogLevel.Information,
@@ -35,6 +38,18 @@ static LoggingExtensions()
3538
eventId: 5,
3639
logLevel: LogLevel.Debug,
3740
formatString: "Session stored; Key:{sessionKey}, Id:{sessionId}, Count:{count}");
41+
_sessionCacheReadException = LoggerMessage.Define<string>(
42+
eventId: 6,
43+
logLevel: LogLevel.Error,
44+
formatString: "Session read exceptionm, Key:{sessionKey}, Exception:{exception}");
45+
_sessionCacheWriteException = LoggerMessage.Define<string, string>(
46+
eventId: 7,
47+
logLevel: LogLevel.Error,
48+
formatString: "Session write exception, Key:{sessionKey}, Id:{sessionId}, Exception:{exception}");
49+
_sessionCacheRefreshException = LoggerMessage.Define<string>(
50+
eventId: 8,
51+
logLevel: LogLevel.Error,
52+
formatString: "Session refresh exception, Key:{sessionKey}, Exception:{exception}");
3853
}
3954

4055
public static void ErrorClosingTheSession(this ILogger logger, Exception exception)
@@ -61,5 +76,20 @@ public static void SessionStored(this ILogger logger, string sessionKey, string
6176
{
6277
_sessionStored(logger, sessionKey, sessionId, count, null);
6378
}
79+
80+
public static void SessionCacheReadException(this ILogger logger, string sessionKey, Exception exception)
81+
{
82+
_sessionCacheReadException(logger, sessionKey, exception);
83+
}
84+
85+
public static void SessionCacheWriteException(this ILogger logger, string sessionKey, string sessionId, Exception exception)
86+
{
87+
_sessionCacheWriteException(logger, sessionKey, sessionId, exception);
88+
}
89+
90+
public static void SessionCacheRefreshException(this ILogger logger, string sessionKey, Exception exception)
91+
{
92+
_sessionCacheRefreshException(logger, sessionKey, exception);
93+
}
6494
}
6595
}

0 commit comments

Comments
 (0)