Skip to content

Commit d95af8f

Browse files
committed
fix check on parameters, plus some cleanup
1 parent f2f744b commit d95af8f

4 files changed

Lines changed: 13 additions & 42 deletions

File tree

src/MongoDbCache/MongoContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ public MongoContext(string connectionString, MongoClientSettings mongoClientSett
8888
var client = mongoClientSettings == null ? new MongoClient(connectionString) : new MongoClient(mongoClientSettings);
8989
var database = client.GetDatabase(databaseName);
9090

91-
IndexKeysDefinition<CacheItem> expireAtIndexModel =
92-
new IndexKeysDefinitionBuilder<CacheItem>().Ascending(p => p.ExpiresAt);
91+
var expireAtIndexModel = new IndexKeysDefinitionBuilder<CacheItem>().Ascending(p => p.ExpiresAt);
9392

9493
_collection = database.GetCollection<CacheItem>(collectionName);
9594

96-
_collection.Indexes.CreateOne(new CreateIndexModel<CacheItem>(expireAtIndexModel, new CreateIndexOptions {
95+
_collection.Indexes.CreateOne(new CreateIndexModel<CacheItem>(expireAtIndexModel, new CreateIndexOptions
96+
{
9797
Background = true
9898
}));
9999
}
@@ -148,7 +148,7 @@ public byte[] GetCacheItem(string key, bool withoutValue)
148148

149149
if (CheckIfExpired(utcNow, cacheItem))
150150
{
151-
Remove(cacheItem.Key);
151+
await RemoveAsync(cacheItem.Key, token);
152152
return null;
153153
}
154154

src/MongoDbCache/MongoDbCache.cs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class MongoDbCache : IDistributedCache
1313
private readonly TimeSpan _defaultScanInterval = TimeSpan.FromMinutes(5);
1414
private readonly MongoContext _mongoContext;
1515

16-
private void ValidateOptions(MongoDbCacheOptions cacheOptions)
16+
private static void ValidateOptions(MongoDbCacheOptions cacheOptions)
1717
{
18-
if (!string.IsNullOrEmpty(cacheOptions.ConnectionString) && cacheOptions.MongoClientSettings == null)
18+
if (!string.IsNullOrEmpty(cacheOptions.ConnectionString) && cacheOptions.MongoClientSettings != null)
1919
{
2020
throw new ArgumentException(
2121
$"Only one of {nameof(cacheOptions.ConnectionString)} and {nameof(cacheOptions.MongoClientSettings)} can be set.");
@@ -51,7 +51,9 @@ public MongoDbCache(IOptions<MongoDbCacheOptions> optionsAccessor)
5151
{
5252
var options = optionsAccessor.Value;
5353
ValidateOptions(options);
54+
5455
_mongoContext = new MongoContext(options.ConnectionString, options.MongoClientSettings, options.DatabaseName, options.CollectionName);
56+
5557
SetScanInterval(options.ExpiredScanInterval);
5658
}
5759

@@ -78,37 +80,7 @@ public void Refresh(string key)
7880
ScanAndDeleteExpired();
7981
}
8082

81-
public async Task<byte[]> GetAsync(string key)
82-
{
83-
var value = await _mongoContext.GetCacheItemAsync(key, withoutValue: false);
84-
85-
ScanAndDeleteExpired();
86-
87-
return value;
88-
}
89-
90-
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options = null)
91-
{
92-
await _mongoContext.SetAsync(key, value, options);
93-
94-
ScanAndDeleteExpired();
95-
}
96-
97-
public async Task RefreshAsync(string key)
98-
{
99-
await _mongoContext.GetCacheItemAsync(key, withoutValue: true);
100-
101-
ScanAndDeleteExpired();
102-
}
103-
104-
public async Task RemoveAsync(string key)
105-
{
106-
await _mongoContext.RemoveAsync(key);
107-
108-
ScanAndDeleteExpired();
109-
}
110-
111-
public async Task<byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken))
83+
public async Task<byte[]> GetAsync(string key, CancellationToken token = default)
11284
{
11385
var value = await _mongoContext.GetCacheItemAsync(key, withoutValue: false, token: token);
11486

@@ -117,21 +89,21 @@ public async Task RemoveAsync(string key)
11789
return value;
11890
}
11991

120-
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
92+
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
12193
{
12294
await _mongoContext.SetAsync(key, value, options, token);
12395

12496
ScanAndDeleteExpired();
12597
}
12698

127-
public async Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
99+
public async Task RefreshAsync(string key, CancellationToken token = default)
128100
{
129101
await _mongoContext.GetCacheItemAsync(key, withoutValue: true, token: token);
130102

131103
ScanAndDeleteExpired();
132104
}
133105

134-
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
106+
public async Task RemoveAsync(string key, CancellationToken token = default)
135107
{
136108
await _mongoContext.RemoveAsync(key, token);
137109

src/MongoDbCache/MongoDbCache.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
5-
<Version>2.4.0</Version>
5+
<Version>2.4.1</Version>
66
<PackageLicenseExpression>MIT</PackageLicenseExpression>
77
<Copyright>Copyright (c) OUTMATIC Alessandro Petrelli &amp; Contributors</Copyright>
88
<Authors>Alessandro Petrelli &amp; Contributors</Authors>

src/MongoDbCache/MongoDbCacheServicesExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Microsoft.Extensions.Caching.Distributed;
33
using Microsoft.Extensions.DependencyInjection;
4-
using Microsoft.Extensions.DependencyInjection.Extensions;
54

65
namespace MongoDbCache
76
{

0 commit comments

Comments
 (0)