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

Commit ccad490

Browse files
committed
More Feedback
1 parent 772a9ce commit ccad490

File tree

1 file changed

+73
-51
lines changed

1 file changed

+73
-51
lines changed

src/Microsoft.AspNetCore.Session/DistributedSession.cs

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Linq;
@@ -26,8 +27,8 @@ public class DistributedSession : ISession
2627
private readonly string _sessionKey;
2728
private readonly TimeSpan _idleTimeout;
2829
private readonly Func<bool> _tryEstablishSession;
29-
private readonly IDictionary<EncodedKey, byte[]> _store;
3030
private readonly ILogger _logger;
31+
private IDictionary<EncodedKey, byte[]> _store;
3132
private bool _isModified;
3233
private bool _loaded;
3334
private bool _isAvailable;
@@ -86,27 +87,19 @@ public string Id
8687
get
8788
{
8889
Load();
89-
if (_isAvailable)
90+
if (_sessionId == null)
9091
{
91-
if (_sessionId == null)
92-
{
93-
_sessionId = new Guid(IdBytes).ToString();
94-
}
95-
return _sessionId;
96-
}
97-
else
98-
{
99-
return string.Empty;
92+
_sessionId = new Guid(IdBytes).ToString();
10093
}
94+
return _sessionId;
10195
}
10296
}
10397

10498
private byte[] IdBytes
10599
{
106100
get
107101
{
108-
Load();
109-
if (_isAvailable)
102+
if (IsAvailable)
110103
{
111104
if (_sessionIdBytes == null)
112105
{
@@ -127,29 +120,14 @@ public IEnumerable<string> Keys
127120
get
128121
{
129122
Load();
130-
if (_isAvailable)
131-
{
132-
return _store.Keys.Select(key => key.KeyString);
133-
}
134-
else
135-
{
136-
return Enumerable.Empty<string>();
137-
}
123+
return _store.Keys.Select(key => key.KeyString);
138124
}
139125
}
140126

141127
public bool TryGetValue(string key, out byte[] value)
142128
{
143129
Load();
144-
if (_isAvailable)
145-
{
146-
return _store.TryGetValue(new EncodedKey(key), out value);
147-
}
148-
else
149-
{
150-
value = default(byte[]);
151-
return false;
152-
}
130+
return _store.TryGetValue(new EncodedKey(key), out value);
153131
}
154132

155133
public void Set(string key, byte[] value)
@@ -159,16 +137,15 @@ public void Set(string key, byte[] value)
159137
throw new ArgumentNullException(nameof(value));
160138
}
161139

162-
var encodedKey = new EncodedKey(key);
163-
if (encodedKey.KeyBytes.Length > KeyLengthLimit)
140+
if (IsAvailable)
164141
{
165-
throw new ArgumentOutOfRangeException(nameof(key),
166-
Resources.FormatException_KeyLengthIsExceeded(KeyLengthLimit));
167-
}
142+
var encodedKey = new EncodedKey(key);
143+
if (encodedKey.KeyBytes.Length > KeyLengthLimit)
144+
{
145+
throw new ArgumentOutOfRangeException(nameof(key),
146+
Resources.FormatException_KeyLengthIsExceeded(KeyLengthLimit));
147+
}
168148

169-
Load();
170-
if (_isAvailable)
171-
{
172149
if (!_tryEstablishSession())
173150
{
174151
throw new InvalidOperationException(Resources.Exception_InvalidSessionEstablishment);
@@ -183,20 +160,14 @@ public void Set(string key, byte[] value)
183160
public void Remove(string key)
184161
{
185162
Load();
186-
if (_isAvailable)
187-
{
188-
_isModified |= _store.Remove(new EncodedKey(key));
189-
}
163+
_isModified |= _store.Remove(new EncodedKey(key));
190164
}
191165

192166
public void Clear()
193167
{
194168
Load();
195-
if (_isAvailable)
196-
{
197-
_isModified |= _store.Count > 0;
198-
_store.Clear();
199-
}
169+
_isModified |= _store.Count > 0;
170+
_store.Clear();
200171
}
201172

202173
private void Load()
@@ -220,6 +191,8 @@ private void Load()
220191
{
221192
_logger.SessionCacheReadException(_sessionKey, exception);
222193
_isAvailable = false;
194+
_sessionId = string.Empty;
195+
_store = new EmptySessionStore();
223196
}
224197
finally
225198
{
@@ -228,8 +201,7 @@ private void Load()
228201
}
229202
}
230203

231-
// TODO: This should throw if called directly, but most other places it should fail silently
232-
// (e.g. TryGetValue should just return null).
204+
// This will throw if called directly and a failure occurs. The user is expected to handle the failures.
233205
public async Task LoadAsync()
234206
{
235207
if (!_loaded)
@@ -268,7 +240,6 @@ public async Task CommitAsync()
268240
return;
269241
}
270242
}
271-
_isModified = false;
272243

273244
var stream = new MemoryStream();
274245
Serialize(stream);
@@ -285,6 +256,8 @@ await _cache.SetAsync(
285256
_logger.SessionCacheWriteException(_sessionKey, Id, exception);
286257
return;
287258
}
259+
260+
_isModified = false;
288261
_logger.SessionStored(_sessionKey, Id, _store.Count);
289262
}
290263
else
@@ -329,7 +302,6 @@ private void Deserialize(Stream content)
329302
{
330303
if (content == null || content.ReadByte() != SerializationRevision)
331304
{
332-
// TODO: Throw?
333305
// Replace the un-readable format.
334306
_isModified = true;
335307
return;
@@ -489,5 +461,55 @@ public override string ToString()
489461
return KeyString;
490462
}
491463
}
464+
465+
private class EmptySessionStore : IDictionary<EncodedKey, byte[]>
466+
{
467+
public byte[] this[EncodedKey key]
468+
{
469+
get
470+
{
471+
return null;
472+
}
473+
474+
set
475+
{
476+
477+
}
478+
}
479+
480+
public int Count { get; } = 0;
481+
482+
public bool IsReadOnly { get; } = false;
483+
484+
public ICollection<EncodedKey> Keys { get; } = new EncodedKey[0];
485+
486+
public ICollection<byte[]> Values { get; } = new byte[0][];
487+
488+
public void Add(KeyValuePair<EncodedKey, byte[]> item) { }
489+
490+
public void Add(EncodedKey key, byte[] value) { }
491+
492+
public void Clear() { }
493+
494+
public bool Contains(KeyValuePair<EncodedKey, byte[]> item) => false;
495+
496+
public bool ContainsKey(EncodedKey key) => false;
497+
498+
public void CopyTo(KeyValuePair<EncodedKey, byte[]>[] array, int arrayIndex) { }
499+
500+
public IEnumerator<KeyValuePair<EncodedKey, byte[]>> GetEnumerator() => Enumerable.Empty<KeyValuePair<EncodedKey, byte[]>>().GetEnumerator();
501+
502+
public bool Remove(KeyValuePair<EncodedKey, byte[]> item) => false;
503+
504+
public bool Remove(EncodedKey key) => false;
505+
506+
public bool TryGetValue(EncodedKey key, out byte[] value)
507+
{
508+
value = null;
509+
return false;
510+
}
511+
512+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
513+
}
492514
}
493515
}

0 commit comments

Comments
 (0)