11namespace EasyCaching . HybridCache
22{
3- using System ;
4- using System . Collections . Generic ;
5- using System . Linq ;
6- using System . Threading ;
7- using System . Threading . Tasks ;
83 using EasyCaching . Core ;
94 using EasyCaching . Core . Bus ;
105 using Microsoft . Extensions . Logging ;
116 using Polly ;
127 using Polly . Fallback ;
138 using Polly . Retry ;
149 using Polly . Wrap ;
10+ using System ;
11+ using System . Collections . Generic ;
12+ using System . Linq ;
13+ using System . Threading ;
14+ using System . Threading . Tasks ;
1515
1616 /// <summary>
1717 /// Hybrid caching provider.
@@ -123,7 +123,7 @@ string name
123123 /// <param name="message">Message.</param>
124124 private void OnMessage ( EasyCachingMessage message )
125125 {
126- // each clients will recive the message, current client should ignore.
126+ // each clients will receive the message, current client should ignore.
127127 if ( ! string . IsNullOrWhiteSpace ( message . Id ) && message . Id . Equals ( _cacheId , StringComparison . OrdinalIgnoreCase ) )
128128 return ;
129129
@@ -136,7 +136,7 @@ private void OnMessage(EasyCachingMessage message)
136136
137137 LogMessage ( $ "remove local cache that pattern is { pattern } ") ;
138138
139- return ;
139+ return ;
140140 }
141141
142142 // remove by prefix
@@ -178,6 +178,11 @@ public bool Exists(string cacheKey)
178178 catch ( Exception ex )
179179 {
180180 LogMessage ( $ "Check cache key exists error [{ cacheKey } ] ", ex ) ;
181+
182+ if ( _options . ThrowIfDistributedCacheError )
183+ {
184+ throw ;
185+ }
181186 }
182187
183188 flag = _localCache . Exists ( cacheKey ) ;
@@ -186,7 +191,7 @@ public bool Exists(string cacheKey)
186191 }
187192
188193 /// <summary>
189- /// Existses the specified cacheKey async.
194+ /// Exists the specified cacheKey async.
190195 /// </summary>
191196 /// <returns>The async.</returns>
192197 /// <param name="cacheKey">Cache key.</param>
@@ -206,6 +211,11 @@ public async Task<bool> ExistsAsync(string cacheKey, CancellationToken cancellat
206211 catch ( Exception ex )
207212 {
208213 LogMessage ( $ "Check cache key [{ cacheKey } ] exists error", ex ) ;
214+
215+ if ( _options . ThrowIfDistributedCacheError )
216+ {
217+ throw ;
218+ }
209219 }
210220
211221 flag = await _localCache . ExistsAsync ( cacheKey , cancellationToken ) ;
@@ -239,12 +249,17 @@ public CacheValue<T> Get<T>(string cacheKey)
239249 catch ( Exception ex )
240250 {
241251 LogMessage ( $ "distributed cache get error, [{ cacheKey } ]", ex ) ;
252+
253+ if ( _options . ThrowIfDistributedCacheError )
254+ {
255+ throw ;
256+ }
242257 }
243258
244259 if ( cacheValue . HasValue )
245260 {
246261 TimeSpan ts = GetExpiration ( cacheKey ) ;
247-
262+
248263 _localCache . Set ( cacheKey , cacheValue . Value , ts ) ;
249264
250265 return cacheValue ;
@@ -282,6 +297,11 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey, CancellationToken
282297 catch ( Exception ex )
283298 {
284299 LogMessage ( $ "distributed cache get error, [{ cacheKey } ]", ex ) ;
300+
301+ if ( _options . ThrowIfDistributedCacheError )
302+ {
303+ throw ;
304+ }
285305 }
286306
287307 if ( cacheValue . HasValue )
@@ -314,6 +334,11 @@ public void Remove(string cacheKey)
314334 catch ( Exception ex )
315335 {
316336 LogMessage ( $ "remove cache key [{ cacheKey } ] error", ex ) ;
337+
338+ if ( _options . ThrowIfDistributedCacheError )
339+ {
340+ throw ;
341+ }
317342 }
318343
319344 _localCache . Remove ( cacheKey ) ;
@@ -340,6 +365,11 @@ public async Task RemoveAsync(string cacheKey, CancellationToken cancellationTok
340365 catch ( Exception ex )
341366 {
342367 LogMessage ( $ "remove cache key [{ cacheKey } ] error", ex ) ;
368+
369+ if ( _options . ThrowIfDistributedCacheError )
370+ {
371+ throw ;
372+ }
343373 }
344374
345375 await _localCache . RemoveAsync ( cacheKey , cancellationToken ) ;
@@ -368,6 +398,11 @@ public void Set<T>(string cacheKey, T cacheValue, TimeSpan expiration)
368398 catch ( Exception ex )
369399 {
370400 LogMessage ( $ "set cache key [{ cacheKey } ] error", ex ) ;
401+
402+ if ( _options . ThrowIfDistributedCacheError )
403+ {
404+ throw ;
405+ }
371406 }
372407
373408 // When create/update cache, send message to bus so that other clients can remove it.
@@ -396,6 +431,11 @@ public async Task SetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration
396431 catch ( Exception ex )
397432 {
398433 LogMessage ( $ "set cache key [{ cacheKey } ] error", ex ) ;
434+
435+ if ( _options . ThrowIfDistributedCacheError )
436+ {
437+ throw ;
438+ }
399439 }
400440
401441 // When create/update cache, send message to bus so that other clients can remove it.
@@ -425,6 +465,11 @@ public bool TrySet<T>(string cacheKey, T cacheValue, TimeSpan expiration)
425465 {
426466 distributedError = true ;
427467 LogMessage ( $ "tryset cache key [{ cacheKey } ] error", ex ) ;
468+
469+ if ( _options . ThrowIfDistributedCacheError )
470+ {
471+ throw ;
472+ }
428473 }
429474
430475 if ( flag && ! distributedError )
@@ -473,6 +518,11 @@ public async Task<bool> TrySetAsync<T>(string cacheKey, T cacheValue, TimeSpan e
473518 {
474519 distributedError = true ;
475520 LogMessage ( $ "tryset cache key [{ cacheKey } ] error", ex ) ;
521+
522+ if ( _options . ThrowIfDistributedCacheError )
523+ {
524+ throw ;
525+ }
476526 }
477527
478528 if ( flag && ! distributedError )
@@ -514,6 +564,11 @@ public void SetAll<T>(IDictionary<string, T> value, TimeSpan expiration)
514564 catch ( Exception ex )
515565 {
516566 LogMessage ( $ "set all from distributed provider error [{ string . Join ( "," , value . Keys ) } ]", ex ) ;
567+
568+ if ( _options . ThrowIfDistributedCacheError )
569+ {
570+ throw ;
571+ }
517572 }
518573
519574 // send message to bus
@@ -539,6 +594,11 @@ public async Task SetAllAsync<T>(IDictionary<string, T> value, TimeSpan expirati
539594 catch ( Exception ex )
540595 {
541596 LogMessage ( $ "set all from distributed provider error [{ string . Join ( "," , value . Keys ) } ]", ex ) ;
597+
598+ if ( _options . ThrowIfDistributedCacheError )
599+ {
600+ throw ;
601+ }
542602 }
543603
544604 // send message to bus
@@ -555,11 +615,16 @@ public void RemoveAll(IEnumerable<string> cacheKeys)
555615
556616 try
557617 {
558- _distributedCache . RemoveAllAsync ( cacheKeys ) ;
618+ _distributedCache . RemoveAll ( cacheKeys ) ;
559619 }
560620 catch ( Exception ex )
561621 {
562622 LogMessage ( $ "remove all from distributed provider error [{ string . Join ( "," , cacheKeys ) } ]", ex ) ;
623+
624+ if ( _options . ThrowIfDistributedCacheError )
625+ {
626+ throw ;
627+ }
563628 }
564629
565630 _localCache . RemoveAll ( cacheKeys ) ;
@@ -585,6 +650,11 @@ public async Task RemoveAllAsync(IEnumerable<string> cacheKeys, CancellationToke
585650 catch ( Exception ex )
586651 {
587652 LogMessage ( $ "remove all async from distributed provider error [{ string . Join ( "," , cacheKeys ) } ]", ex ) ;
653+
654+ if ( _options . ThrowIfDistributedCacheError )
655+ {
656+ throw ;
657+ }
588658 }
589659
590660 await _localCache . RemoveAllAsync ( cacheKeys , cancellationToken ) ;
@@ -620,6 +690,11 @@ public CacheValue<T> Get<T>(string cacheKey, Func<T> dataRetriever, TimeSpan exp
620690 catch ( Exception ex )
621691 {
622692 LogMessage ( $ "get with data retriever from distributed provider error [{ cacheKey } ]", ex ) ;
693+
694+ if ( _options . ThrowIfDistributedCacheError )
695+ {
696+ throw ;
697+ }
623698 }
624699
625700 if ( result . HasValue )
@@ -662,13 +737,18 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey, Func<Task<T>> data
662737 catch ( Exception ex )
663738 {
664739 LogMessage ( $ "get async with data retriever from distributed provider error [{ cacheKey } ]", ex ) ;
740+
741+ if ( _options . ThrowIfDistributedCacheError )
742+ {
743+ throw ;
744+ }
665745 }
666746
667747 if ( result . HasValue )
668748 {
669749 TimeSpan ts = await GetExpirationAsync ( cacheKey , cancellationToken ) ;
670750
671- _localCache . Set ( cacheKey , result . Value , ts ) ;
751+ await _localCache . SetAsync ( cacheKey , result . Value , ts , cancellationToken ) ;
672752
673753 return result ;
674754 }
@@ -691,6 +771,11 @@ public void RemoveByPrefix(string prefix)
691771 catch ( Exception ex )
692772 {
693773 LogMessage ( $ "remove by prefix [{ prefix } ] error", ex ) ;
774+
775+ if ( _options . ThrowIfDistributedCacheError )
776+ {
777+ throw ;
778+ }
694779 }
695780
696781 _localCache . RemoveByPrefix ( prefix ) ;
@@ -716,14 +801,19 @@ public async Task RemoveByPrefixAsync(string prefix, CancellationToken cancellat
716801 catch ( Exception ex )
717802 {
718803 LogMessage ( $ "remove by prefix [{ prefix } ] error", ex ) ;
804+
805+ if ( _options . ThrowIfDistributedCacheError )
806+ {
807+ throw ;
808+ }
719809 }
720810
721- await _localCache . RemoveByPrefixAsync ( prefix ) ;
811+ await _localCache . RemoveByPrefixAsync ( prefix , cancellationToken ) ;
722812
723813 // send message to bus in order to notify other clients.
724814 await _busAsyncWrap . ExecuteAsync ( async ( ct ) => await _bus . PublishAsync ( _options . TopicName , new EasyCachingMessage { Id = _cacheId , CacheKeys = new string [ ] { prefix } , IsPrefix = true } , ct ) , cancellationToken ) ;
725815 }
726-
816+
727817 /// <summary>
728818 /// Removes the by pattern async.
729819 /// </summary>
@@ -741,14 +831,19 @@ public async Task RemoveByPatternAsync(string pattern, CancellationToken cancell
741831 catch ( Exception ex )
742832 {
743833 LogMessage ( $ "remove by pattern [{ pattern } ] error", ex ) ;
834+
835+ if ( _options . ThrowIfDistributedCacheError )
836+ {
837+ throw ;
838+ }
744839 }
745840
746- await _localCache . RemoveByPatternAsync ( pattern ) ;
841+ await _localCache . RemoveByPatternAsync ( pattern , cancellationToken ) ;
747842
748843 // send message to bus in order to notify other clients.
749- await _busAsyncWrap . ExecuteAsync ( async ( ct ) => await _bus . PublishAsync ( _options . TopicName , new EasyCachingMessage { Id = _cacheId , CacheKeys = new string [ ] { pattern } , IsPattern = true } , ct ) , cancellationToken ) ;
844+ await _busAsyncWrap . ExecuteAsync ( async ( ct ) => await _bus . PublishAsync ( _options . TopicName , new EasyCachingMessage { Id = _cacheId , CacheKeys = new string [ ] { pattern } , IsPattern = true } , ct ) , cancellationToken ) ;
750845 }
751-
846+
752847 /// <summary>
753848 /// Removes the by pattern.
754849 /// </summary>
@@ -765,12 +860,17 @@ public void RemoveByPattern(string pattern)
765860 catch ( Exception ex )
766861 {
767862 LogMessage ( $ "remove by pattern [{ pattern } ] error", ex ) ;
863+
864+ if ( _options . ThrowIfDistributedCacheError )
865+ {
866+ throw ;
867+ }
768868 }
769869
770870 _localCache . RemoveByPattern ( pattern ) ;
771871
772872 // send message to bus
773- _busSyncWrap . Execute ( ( ) => _bus . Publish ( _options . TopicName , new EasyCachingMessage { Id = _cacheId , CacheKeys = new string [ ] { pattern } , IsPattern = true } ) ) ;
873+ _busSyncWrap . Execute ( ( ) => _bus . Publish ( _options . TopicName , new EasyCachingMessage { Id = _cacheId , CacheKeys = new string [ ] { pattern } , IsPattern = true } ) ) ;
774874 }
775875
776876 /// <summary>
@@ -855,12 +955,17 @@ public async Task<object> GetAsync(string cacheKey, Type type, CancellationToken
855955 catch ( Exception ex )
856956 {
857957 LogMessage ( $ "distributed cache get error, [{ cacheKey } ]", ex ) ;
958+
959+ if ( _options . ThrowIfDistributedCacheError )
960+ {
961+ throw ;
962+ }
858963 }
859964
860965 if ( cacheValue != null )
861966 {
862967 TimeSpan ts = await GetExpirationAsync ( cacheKey , cancellationToken ) ;
863-
968+
864969 await _localCache . SetAsync ( cacheKey , cacheValue , ts , cancellationToken ) ;
865970
866971 return cacheValue ;
0 commit comments