Skip to content

Commit b31e1e5

Browse files
committed
clean up and cosmetic changes
1 parent d95af8f commit b31e1e5

5 files changed

Lines changed: 30 additions & 138 deletions

File tree

src/MongoDbCache.Tests/TestDistributedCache.cs

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,78 +9,36 @@ namespace MongoDbCache.Tests
99
internal class TestDistributedCache : IDistributedCache
1010
{
1111
public void Connect()
12-
{
13-
throw new NotImplementedException();
14-
}
12+
=> throw new NotImplementedException();
1513

1614
public Task ConnectAsync()
17-
{
18-
throw new NotImplementedException();
19-
}
15+
=> throw new NotImplementedException();
2016

2117
public byte[] Get(string key)
22-
{
23-
throw new NotImplementedException();
24-
}
18+
=> throw new NotImplementedException();
2519

26-
public Task<byte[]> GetAsync(string key)
27-
{
28-
throw new NotImplementedException();
29-
}
30-
31-
public Task<byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken))
32-
{
33-
throw new NotImplementedException();
34-
}
20+
public Task<byte[]> GetAsync(string key, CancellationToken token = default)
21+
=> throw new NotImplementedException();
3522

3623
public void Refresh(string key)
37-
{
38-
throw new NotImplementedException();
39-
}
40-
41-
public Task RefreshAsync(string key)
42-
{
43-
throw new NotImplementedException();
44-
}
24+
=> throw new NotImplementedException();
4525

46-
public Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
47-
{
48-
throw new NotImplementedException();
49-
}
26+
public Task RefreshAsync(string key, CancellationToken token = default)
27+
=> throw new NotImplementedException();
5028

5129
public void Remove(string key)
52-
{
53-
throw new NotImplementedException();
54-
}
30+
=> throw new NotImplementedException();
5531

56-
public Task RemoveAsync(string key)
57-
{
58-
throw new NotImplementedException();
59-
}
60-
61-
public Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
62-
{
63-
throw new NotImplementedException();
64-
}
32+
public Task RemoveAsync(string key, CancellationToken token = default)
33+
=> throw new NotImplementedException();
6534

6635
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
67-
{
68-
throw new NotImplementedException();
69-
}
70-
71-
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options)
72-
{
73-
throw new NotImplementedException();
74-
}
36+
=> throw new NotImplementedException();
7537

76-
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
77-
{
78-
throw new NotImplementedException();
79-
}
38+
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
39+
=> throw new NotImplementedException();
8040

8141
public bool TryGetValue(string key, out Stream value)
82-
{
83-
throw new NotImplementedException();
84-
}
42+
=> throw new NotImplementedException();
8543
}
8644
}

src/MongoDbCache/CacheItem.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ internal class CacheItem
2121
public double? SlidingExpirationInSeconds { get; }
2222

2323
[BsonConstructor]
24-
public CacheItem(string key, byte[] value, DateTimeOffset? expiresAt, DateTimeOffset? absoluteExpiration,
25-
double? slidingExpirationInSeconds)
24+
public CacheItem(string key, byte[] value, DateTimeOffset? expiresAt, DateTimeOffset? absoluteExpiration, double? slidingExpirationInSeconds)
2625
{
2726
Key = key;
2827
Value = value;
@@ -32,8 +31,7 @@ public CacheItem(string key, byte[] value, DateTimeOffset? expiresAt, DateTimeOf
3231
}
3332

3433
[BsonConstructor]
35-
public CacheItem(string key, DateTimeOffset? expiresAt, DateTimeOffset? absoluteExpiration,
36-
double? slidingExpirationInSeconds)
34+
public CacheItem(string key, DateTimeOffset? expiresAt, DateTimeOffset? absoluteExpiration, double? slidingExpirationInSeconds)
3735
: this(key, null, expiresAt, absoluteExpiration, slidingExpirationInSeconds)
3836
{
3937

src/MongoDbCache/MongoContext.cs

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,30 @@ internal class MongoContext
1111
private readonly IMongoCollection<CacheItem> _collection;
1212

1313
private static FilterDefinition<CacheItem> FilterByKey(string key)
14-
{
15-
return Builders<CacheItem>.Filter.Eq(x => x.Key, key);
16-
}
14+
=> Builders<CacheItem>.Filter.Eq(x => x.Key, key);
1715

1816
private static FilterDefinition<CacheItem> FilterByExpiresAtNotNull()
19-
{
20-
return Builders<CacheItem>.Filter.Ne(x => x.ExpiresAt, null);
21-
}
17+
=> Builders<CacheItem>.Filter.Ne(x => x.ExpiresAt, null);
2218

2319
private IFindFluent<CacheItem, CacheItem> GetItemQuery(string key, bool withoutValue)
2420
{
2521
var query = _collection.Find(FilterByKey(key));
2622
if (withoutValue)
27-
{
2823
query = query.Project<CacheItem>(Builders<CacheItem>.Projection.Exclude(x => x.Value));
29-
}
3024

3125
return query;
3226
}
3327

3428
private static bool CheckIfExpired(DateTimeOffset utcNow, CacheItem cacheItem)
35-
{
36-
return cacheItem?.ExpiresAt <= utcNow;
37-
}
29+
=> cacheItem?.ExpiresAt <= utcNow;
3830

3931
private static DateTimeOffset? GetExpiresAt(DateTimeOffset utcNow, double? slidingExpirationInSeconds, DateTimeOffset? absoluteExpiration)
4032
{
4133
if (slidingExpirationInSeconds == null && absoluteExpiration == null)
42-
{
4334
return null;
44-
}
4535

4636
if (slidingExpirationInSeconds == null)
47-
{
4837
return absoluteExpiration;
49-
}
5038

5139
var seconds = slidingExpirationInSeconds.GetValueOrDefault();
5240

@@ -58,9 +46,7 @@ private static bool CheckIfExpired(DateTimeOffset utcNow, CacheItem cacheItem)
5846
private CacheItem UpdateExpiresAtIfRequired(DateTimeOffset utcNow, CacheItem cacheItem)
5947
{
6048
if (cacheItem.ExpiresAt == null)
61-
{
6249
return cacheItem;
63-
}
6450

6551
var absoluteExpiration = GetExpiresAt(utcNow, cacheItem.SlidingExpirationInSeconds, cacheItem.AbsoluteExpiration);
6652
_collection.UpdateOne(FilterByKey(cacheItem.Key) & FilterByExpiresAtNotNull(),
@@ -72,9 +58,7 @@ private CacheItem UpdateExpiresAtIfRequired(DateTimeOffset utcNow, CacheItem cac
7258
private async Task<CacheItem> UpdateExpiresAtIfRequiredAsync(DateTimeOffset utcNow, CacheItem cacheItem)
7359
{
7460
if (cacheItem.ExpiresAt == null)
75-
{
7661
return cacheItem;
77-
}
7862

7963
var absoluteExpiration = GetExpiresAt(utcNow, cacheItem.SlidingExpirationInSeconds, cacheItem.AbsoluteExpiration);
8064
await _collection.UpdateOneAsync(FilterByKey(cacheItem.Key) & FilterByExpiresAtNotNull(),
@@ -99,25 +83,19 @@ public MongoContext(string connectionString, MongoClientSettings mongoClientSett
9983
}
10084

10185
public void DeleteExpired(DateTimeOffset utcNow)
102-
{
103-
_collection.DeleteMany(Builders<CacheItem>.Filter.Lte(x => x.ExpiresAt, utcNow));
104-
}
86+
=> _collection.DeleteMany(Builders<CacheItem>.Filter.Lte(x => x.ExpiresAt, utcNow));
10587

10688
public byte[] GetCacheItem(string key, bool withoutValue)
10789
{
10890
var utcNow = DateTimeOffset.UtcNow;
10991

11092
if (key == null)
111-
{
11293
return null;
113-
}
11494

11595
var query = GetItemQuery(key, withoutValue);
11696
var cacheItem = query.SingleOrDefault();
11797
if (cacheItem == null)
118-
{
11998
return null;
120-
}
12199

122100
if (CheckIfExpired(utcNow, cacheItem))
123101
{
@@ -130,21 +108,17 @@ public byte[] GetCacheItem(string key, bool withoutValue)
130108
return cacheItem?.Value;
131109
}
132110

133-
public async Task<byte[]> GetCacheItemAsync(string key, bool withoutValue, CancellationToken token = default(CancellationToken))
111+
public async Task<byte[]> GetCacheItemAsync(string key, bool withoutValue, CancellationToken token = default)
134112
{
135113
var utcNow = DateTimeOffset.UtcNow;
136114

137115
if (key == null)
138-
{
139116
return null;
140-
}
141117

142118
var query = GetItemQuery(key, withoutValue);
143119
var cacheItem = await query.SingleOrDefaultAsync(token);
144120
if (cacheItem == null)
145-
{
146121
return null;
147-
}
148122

149123
if (CheckIfExpired(utcNow, cacheItem))
150124
{
@@ -162,27 +136,19 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options =
162136
var utcNow = DateTimeOffset.UtcNow;
163137

164138
if (key == null)
165-
{
166139
throw new ArgumentNullException(nameof(key));
167-
}
168140

169141
if (value == null)
170-
{
171142
throw new ArgumentNullException(nameof(value));
172-
}
173143

174144
var absolutExpiration = options?.AbsoluteExpiration;
175145
var slidingExpirationInSeconds = options?.SlidingExpiration?.TotalSeconds;
176146

177147
if (options?.AbsoluteExpirationRelativeToNow != null)
178-
{
179148
absolutExpiration = utcNow.Add(options.AbsoluteExpirationRelativeToNow.Value);
180-
}
181149

182150
if (absolutExpiration <= utcNow)
183-
{
184151
throw new InvalidOperationException("The absolute expiration value must be in the future.");
185-
}
186152

187153
var expiresAt = GetExpiresAt(utcNow, slidingExpirationInSeconds, absolutExpiration);
188154
var cacheItem = new CacheItem(key, value, expiresAt, absolutExpiration, slidingExpirationInSeconds);
@@ -193,32 +159,24 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options =
193159
});
194160
}
195161

196-
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options = null, CancellationToken token = default(CancellationToken))
162+
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options = null, CancellationToken token = default)
197163
{
198164
var utcNow = DateTimeOffset.UtcNow;
199165

200166
if (key == null)
201-
{
202167
throw new ArgumentNullException(nameof(key));
203-
}
204168

205169
if (value == null)
206-
{
207170
throw new ArgumentNullException(nameof(value));
208-
}
209171

210172
var absolutExpiration = options?.AbsoluteExpiration;
211173
var slidingExpirationInSeconds = options?.SlidingExpiration?.TotalSeconds;
212174

213175
if (options?.AbsoluteExpirationRelativeToNow != null)
214-
{
215176
absolutExpiration = utcNow.Add(options.AbsoluteExpirationRelativeToNow.Value);
216-
}
217177

218178
if (absolutExpiration <= utcNow)
219-
{
220179
throw new InvalidOperationException("The absolute expiration value must be in the future.");
221-
}
222180

223181
var expiresAt = GetExpiresAt(utcNow, slidingExpirationInSeconds, absolutExpiration);
224182
var cacheItem = new CacheItem(key, value, expiresAt, absolutExpiration, slidingExpirationInSeconds);
@@ -230,13 +188,9 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options =
230188
}
231189

232190
public void Remove(string key)
233-
{
234-
_collection.DeleteOne(FilterByKey(key));
235-
}
191+
=> _collection.DeleteOne(FilterByKey(key));
236192

237-
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
238-
{
239-
await _collection.DeleteOneAsync(FilterByKey(key), token);
240-
}
193+
public async Task RemoveAsync(string key, CancellationToken token = default)
194+
=> await _collection.DeleteOneAsync(FilterByKey(key), token);
241195
}
242196
}

src/MongoDbCache/MongoDbCache.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,16 @@ public class MongoDbCache : IDistributedCache
1616
private static void ValidateOptions(MongoDbCacheOptions cacheOptions)
1717
{
1818
if (!string.IsNullOrEmpty(cacheOptions.ConnectionString) && cacheOptions.MongoClientSettings != null)
19-
{
20-
throw new ArgumentException(
21-
$"Only one of {nameof(cacheOptions.ConnectionString)} and {nameof(cacheOptions.MongoClientSettings)} can be set.");
22-
}
19+
throw new ArgumentException($"Only one of {nameof(cacheOptions.ConnectionString)} and {nameof(cacheOptions.MongoClientSettings)} can be set.");
2320

2421
if (string.IsNullOrEmpty(cacheOptions.ConnectionString) && cacheOptions.MongoClientSettings == null)
25-
{
26-
throw new ArgumentException(
27-
$"{nameof(cacheOptions.ConnectionString)} or {nameof(cacheOptions.MongoClientSettings)} cannot be empty or null.");
28-
}
22+
throw new ArgumentException($"{nameof(cacheOptions.ConnectionString)} or {nameof(cacheOptions.MongoClientSettings)} cannot be empty or null.");
2923

3024
if (string.IsNullOrEmpty(cacheOptions.DatabaseName))
31-
{
32-
throw new ArgumentException(
33-
$"{nameof(cacheOptions.DatabaseName)} cannot be empty or null.");
34-
}
25+
throw new ArgumentException($"{nameof(cacheOptions.DatabaseName)} cannot be empty or null.");
3526

3627
if (string.IsNullOrEmpty(cacheOptions.CollectionName))
37-
{
38-
throw new ArgumentException(
39-
$"{nameof(cacheOptions.CollectionName)} cannot be empty or null.");
40-
}
28+
throw new ArgumentException($"{nameof(cacheOptions.CollectionName)} cannot be empty or null.");
4129
}
4230

4331
private void SetScanInterval(TimeSpan? scanInterval)
@@ -122,13 +110,11 @@ private void ScanAndDeleteExpired()
122110
var utcNow = DateTimeOffset.UtcNow;
123111

124112
if (_lastScan.Add(_scanInterval) < utcNow)
125-
{
126113
Task.Run(() =>
127114
{
128115
_lastScan = utcNow;
129116
_mongoContext.DeleteExpired(utcNow);
130117
});
131-
}
132118
}
133119
}
134120
}

src/MongoDbCache/MongoDbCacheServicesExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@ public static class MongoDbCacheServicesExtensions
1616
public static IServiceCollection AddMongoDbCache(this IServiceCollection services, Action<MongoDbCacheOptions> setupAction)
1717
{
1818
if (services == null)
19-
{
2019
throw new ArgumentNullException(nameof(services));
21-
}
2220

2321
if (setupAction == null)
24-
{
2522
throw new ArgumentNullException(nameof(setupAction));
26-
}
2723

2824
services.AddOptions();
2925
services.Configure(setupAction);

0 commit comments

Comments
 (0)