@@ -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
0 commit comments