44using System ;
55using System . Threading . Tasks ;
66using Microsoft . Extensions . Caching . Distributed ;
7- using Microsoft . Extensions . Logging ;
87using Microsoft . Extensions . Options ;
98using StackExchange . Redis ;
109
@@ -31,16 +30,11 @@ public class RedisCache : IDistributedCache, IDisposable
3130
3231 private ConnectionMultiplexer _connection ;
3332 private IDatabase _cache ;
34- private readonly ILogger _logger ;
3533
3634 private readonly RedisCacheOptions _options ;
3735 private readonly string _instance ;
3836
3937 public RedisCache ( IOptions < RedisCacheOptions > optionsAccessor )
40- : this ( optionsAccessor , loggerFactory : null )
41- { }
42-
43- public RedisCache ( IOptions < RedisCacheOptions > optionsAccessor , ILoggerFactory loggerFactory )
4438 {
4539 if ( optionsAccessor == null )
4640 {
@@ -51,7 +45,6 @@ public RedisCache(IOptions<RedisCacheOptions> optionsAccessor, ILoggerFactory lo
5145
5246 // This allows partitioning a single backend cache for use with multiple apps/services.
5347 _instance = _options . InstanceName ?? string . Empty ;
54- _logger = loggerFactory ? . CreateLogger < RedisCache > ( ) ;
5548 }
5649
5750 public byte [ ] Get ( string key )
@@ -91,27 +84,20 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
9184 throw new ArgumentNullException ( nameof ( options ) ) ;
9285 }
9386
94- try
95- {
96- Connect ( ) ;
87+ Connect ( ) ;
9788
98- var creationTime = DateTimeOffset . UtcNow ;
89+ var creationTime = DateTimeOffset . UtcNow ;
9990
100- var absoluteExpiration = GetAbsoluteExpiration ( creationTime , options ) ;
91+ var absoluteExpiration = GetAbsoluteExpiration ( creationTime , options ) ;
10192
102- var result = _cache . ScriptEvaluate ( SetScript , new RedisKey [ ] { _instance + key } ,
103- new RedisValue [ ]
104- {
93+ var result = _cache . ScriptEvaluate ( SetScript , new RedisKey [ ] { _instance + key } ,
94+ new RedisValue [ ]
95+ {
10596 absoluteExpiration ? . Ticks ?? NotPresent ,
10697 options . SlidingExpiration ? . Ticks ?? NotPresent ,
10798 GetExpirationInSeconds ( creationTime , absoluteExpiration , options ) ?? NotPresent ,
10899 value
109- } ) ;
110- }
111- catch ( Exception ex )
112- {
113- LogSuppressedException ( ex ) ;
114- }
100+ } ) ;
115101 }
116102
117103 public async Task SetAsync ( string key , byte [ ] value , DistributedCacheEntryOptions options )
@@ -131,27 +117,20 @@ public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOption
131117 throw new ArgumentNullException ( nameof ( options ) ) ;
132118 }
133119
134- try
135- {
136- await ConnectAsync ( ) ;
120+ await ConnectAsync ( ) ;
137121
138- var creationTime = DateTimeOffset . UtcNow ;
122+ var creationTime = DateTimeOffset . UtcNow ;
139123
140- var absoluteExpiration = GetAbsoluteExpiration ( creationTime , options ) ;
124+ var absoluteExpiration = GetAbsoluteExpiration ( creationTime , options ) ;
141125
142- await _cache . ScriptEvaluateAsync ( SetScript , new RedisKey [ ] { _instance + key } ,
143- new RedisValue [ ]
144- {
126+ await _cache . ScriptEvaluateAsync ( SetScript , new RedisKey [ ] { _instance + key } ,
127+ new RedisValue [ ]
128+ {
145129 absoluteExpiration ? . Ticks ?? NotPresent ,
146130 options . SlidingExpiration ? . Ticks ?? NotPresent ,
147131 GetExpirationInSeconds ( creationTime , absoluteExpiration , options ) ?? NotPresent ,
148132 value
149- } ) ;
150- }
151- catch ( Exception ex )
152- {
153- LogSuppressedException ( ex ) ;
154- }
133+ } ) ;
155134 }
156135
157136 public void Refresh ( string key )
@@ -199,41 +178,34 @@ private byte[] GetAndRefresh(string key, bool getData)
199178 throw new ArgumentNullException ( nameof ( key ) ) ;
200179 }
201180
202- try
203- {
204- Connect ( ) ;
181+ Connect ( ) ;
205182
206- // This also resets the LRU status as desired.
207- // TODO: Can this be done in one operation on the server side? Probably, the trick would just be the DateTimeOffset math.
208- RedisValue [ ] results ;
209- if ( getData )
210- {
211- results = _cache . HashMemberGet ( _instance + key , AbsoluteExpirationKey , SlidingExpirationKey , DataKey ) ;
212- }
213- else
214- {
215- results = _cache . HashMemberGet ( _instance + key , AbsoluteExpirationKey , SlidingExpirationKey ) ;
216- }
217-
218- // TODO: Error handling
219- if ( results . Length >= 2 )
220- {
221- // Note we always get back two results, even if they are all null.
222- // These operations will no-op in the null scenario.
223- DateTimeOffset ? absExpr ;
224- TimeSpan ? sldExpr ;
225- MapMetadata ( results , out absExpr , out sldExpr ) ;
226- Refresh ( key , absExpr , sldExpr ) ;
227- }
183+ // This also resets the LRU status as desired.
184+ // TODO: Can this be done in one operation on the server side? Probably, the trick would just be the DateTimeOffset math.
185+ RedisValue [ ] results ;
186+ if ( getData )
187+ {
188+ results = _cache . HashMemberGet ( _instance + key , AbsoluteExpirationKey , SlidingExpirationKey , DataKey ) ;
189+ }
190+ else
191+ {
192+ results = _cache . HashMemberGet ( _instance + key , AbsoluteExpirationKey , SlidingExpirationKey ) ;
193+ }
228194
229- if ( results . Length >= 3 && results [ 2 ] . HasValue )
230- {
231- return results [ 2 ] ;
232- }
195+ // TODO: Error handling
196+ if ( results . Length >= 2 )
197+ {
198+ // Note we always get back two results, even if they are all null.
199+ // These operations will no-op in the null scenario.
200+ DateTimeOffset ? absExpr ;
201+ TimeSpan ? sldExpr ;
202+ MapMetadata ( results , out absExpr , out sldExpr ) ;
203+ Refresh ( key , absExpr , sldExpr ) ;
233204 }
234- catch ( Exception ex )
205+
206+ if ( results . Length >= 3 && results [ 2 ] . HasValue )
235207 {
236- LogSuppressedException ( ex ) ;
208+ return results [ 2 ] ;
237209 }
238210
239211 return null ;
@@ -246,41 +218,34 @@ private async Task<byte[]> GetAndRefreshAsync(string key, bool getData)
246218 throw new ArgumentNullException ( nameof ( key ) ) ;
247219 }
248220
249- try
250- {
251- await ConnectAsync ( ) ;
252-
253- // This also resets the LRU status as desired.
254- // TODO: Can this be done in one operation on the server side? Probably, the trick would just be the DateTimeOffset math.
255- RedisValue [ ] results ;
256- if ( getData )
257- {
258- results = await _cache . HashMemberGetAsync ( _instance + key , AbsoluteExpirationKey , SlidingExpirationKey , DataKey ) ;
259- }
260- else
261- {
262- results = await _cache . HashMemberGetAsync ( _instance + key , AbsoluteExpirationKey , SlidingExpirationKey ) ;
263- }
221+ await ConnectAsync ( ) ;
264222
265- // TODO: Error handling
266- if ( results . Length >= 2 )
267- {
268- // Note we always get back two results, even if they are all null.
269- // These operations will no-op in the null scenario.
270- DateTimeOffset ? absExpr ;
271- TimeSpan ? sldExpr ;
272- MapMetadata ( results , out absExpr , out sldExpr ) ;
273- await RefreshAsync ( key , absExpr , sldExpr ) ;
274- }
223+ // This also resets the LRU status as desired.
224+ // TODO: Can this be done in one operation on the server side? Probably, the trick would just be the DateTimeOffset math.
225+ RedisValue [ ] results ;
226+ if ( getData )
227+ {
228+ results = await _cache . HashMemberGetAsync ( _instance + key , AbsoluteExpirationKey , SlidingExpirationKey , DataKey ) ;
229+ }
230+ else
231+ {
232+ results = await _cache . HashMemberGetAsync ( _instance + key , AbsoluteExpirationKey , SlidingExpirationKey ) ;
233+ }
275234
276- if ( results . Length >= 3 && results [ 2 ] . HasValue )
277- {
278- return results [ 2 ] ;
279- }
235+ // TODO: Error handling
236+ if ( results . Length >= 2 )
237+ {
238+ // Note we always get back two results, even if they are all null.
239+ // These operations will no-op in the null scenario.
240+ DateTimeOffset ? absExpr ;
241+ TimeSpan ? sldExpr ;
242+ MapMetadata ( results , out absExpr , out sldExpr ) ;
243+ await RefreshAsync ( key , absExpr , sldExpr ) ;
280244 }
281- catch ( Exception ex )
245+
246+ if ( results . Length >= 3 && results [ 2 ] . HasValue )
282247 {
283- LogSuppressedException ( ex ) ;
248+ return results [ 2 ] ;
284249 }
285250
286251 return null ;
@@ -293,16 +258,10 @@ public void Remove(string key)
293258 throw new ArgumentNullException ( nameof ( key ) ) ;
294259 }
295260
296- try
297- {
298- Connect ( ) ;
261+ Connect ( ) ;
299262
300- _cache . KeyDelete ( _instance + key ) ;
301- }
302- catch ( Exception ex )
303- {
304- LogSuppressedException ( ex ) ;
305- }
263+ _cache . KeyDelete ( _instance + key ) ;
264+ // TODO: Error handling
306265 }
307266
308267 public async Task RemoveAsync ( string key )
@@ -311,16 +270,11 @@ public async Task RemoveAsync(string key)
311270 {
312271 throw new ArgumentNullException ( nameof ( key ) ) ;
313272 }
314- try
315- {
316- await ConnectAsync ( ) ;
317273
318- await _cache . KeyDeleteAsync ( _instance + key ) ;
319- }
320- catch ( Exception ex )
321- {
322- LogSuppressedException ( ex ) ;
323- }
274+ await ConnectAsync ( ) ;
275+
276+ await _cache . KeyDeleteAsync ( _instance + key ) ;
277+ // TODO: Error handling
324278 }
325279
326280 private void MapMetadata ( RedisValue [ ] results , out DateTimeOffset ? absoluteExpiration , out TimeSpan ? slidingExpiration )
@@ -426,9 +380,6 @@ private async Task RefreshAsync(string key, DateTimeOffset? absExpr, TimeSpan? s
426380 return absoluteExpiration ;
427381 }
428382
429- private void LogSuppressedException ( Exception ex )
430- => _logger ? . ExceptionSuppressed ( ex ) ;
431-
432383 public void Dispose ( )
433384 {
434385 if ( _connection != null )
0 commit comments