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