@@ -39,6 +39,35 @@ private static bool TryGetValue<T>(this ICacheProvider cacheProvider, string key
3939 return true ;
4040 }
4141
42+ private static void SetValue < T > ( this ICacheProvider cacheProvider , string hashedKey , string key , QueryResult < T > value , CacheItemPriority priority , TimeSpan cacheExpiration , ILogger logger )
43+ {
44+ if ( cacheProvider == null )
45+ throw new ArgumentNullException ( nameof ( cacheProvider ) ) ;
46+
47+ if ( hashedKey == null )
48+ throw new ArgumentNullException ( nameof ( hashedKey ) ) ;
49+
50+ if ( key == null )
51+ throw new ArgumentNullException ( nameof ( key ) ) ;
52+
53+ if ( logger == null )
54+ throw new ArgumentNullException ( nameof ( logger ) ) ;
55+
56+ if ( cacheExpiration == null )
57+ throw new ArgumentNullException ( nameof ( cacheExpiration ) ) ;
58+
59+ logger . Debug ( cacheExpiration != TimeSpan . Zero
60+ ? $ "Setting up cache for '{ hashedKey } ' expire handling in { cacheExpiration . TotalSeconds } seconds"
61+ : $ "Setting up cache for '{ hashedKey } '") ;
62+
63+ cacheProvider . Cache . Set (
64+ hashedKey ,
65+ value ,
66+ priority ,
67+ cacheExpiration ,
68+ reason => logger . Debug ( $ "Cache for '{ hashedKey } ' has expired. Evicting from cache for '{ reason } '") ) ;
69+ }
70+
4271 private static QueryResult < T > GetOrSet < T > ( this ICacheProvider cacheProvider , string key , Func < QueryResult < T > > getter , CacheItemPriority priority , TimeSpan cacheExpiration , ILogger logger )
4372 {
4473 if ( cacheProvider == null )
@@ -56,7 +85,7 @@ private static QueryResult<T> GetOrSet<T>(this ICacheProvider cacheProvider, str
5685 if ( cacheExpiration == null )
5786 throw new ArgumentNullException ( nameof ( cacheExpiration ) ) ;
5887
59- var hashedKey = $ " { CacheProviderManager . CachePrefix } _ { CacheProviderManager . GlobalCachingPrefixCounter } _ { key . ToSHA256 ( ) } " ;
88+ var hashedKey = FormatHashKey ( key ) ;
6089
6190 if ( ! cacheProvider . TryGetValue < QueryResult < T > > ( hashedKey , out var value ) )
6291 {
@@ -65,16 +94,7 @@ private static QueryResult<T> GetOrSet<T>(this ICacheProvider cacheProvider, str
6594 if ( value == null )
6695 return default ( QueryResult < T > ) ;
6796
68- logger . Debug ( cacheExpiration != TimeSpan . Zero
69- ? $ "Setting up cache for { hashedKey } expire handling in { cacheExpiration . TotalSeconds } seconds"
70- : $ "Setting up cache for { hashedKey } ") ;
71-
72- cacheProvider . Cache . Set (
73- hashedKey ,
74- value ,
75- priority ,
76- cacheExpiration ,
77- reason => logger . Debug ( $ "Cache for { hashedKey } has expired. Evicting from cache for { reason } ") ) ;
97+ cacheProvider . SetValue ( hashedKey , key , value , priority , cacheExpiration , logger ) ;
7898 }
7999 else
80100 {
@@ -105,18 +125,7 @@ public static QueryResult<IEnumerable<T>> GetOrSetExecuteQuery<T>(this ICachePro
105125 if ( projector == null )
106126 throw new ArgumentNullException ( nameof ( projector ) ) ;
107127
108- var sb = new StringBuilder ( ) ;
109-
110- sb . Append ( $ "GetOrSetExecuteQuery<{ typeof ( T ) . Name } >: [ \n \t Sql = { sql } ,") ;
111-
112- if ( parameters != null && parameters . Any ( ) )
113- {
114- sb . Append ( $ "\n \t Parameters = { string . Join ( ", " , parameters . Select ( x => x . ToString ( ) ) . ToArray ( ) ) } ,") ;
115- }
116-
117- sb . Append ( $ "\n \t CommandType = { cmdType } ]") ;
118-
119- var key = sb . ToString ( ) ;
128+ var key = FormatGetOrSetExecuteQueryKey < T > ( sql , cmdType , parameters ) ;
120129
121130 return cacheProvider . GetOrSet < IEnumerable < T > > ( key , getter , logger ) ;
122131 }
@@ -129,22 +138,11 @@ public static QueryResult<int> GetOrSetExecuteQuery<T>(this ICacheProvider cache
129138 if ( sql == null )
130139 throw new ArgumentNullException ( nameof ( sql ) ) ;
131140
132- var sb = new StringBuilder ( ) ;
133-
134- sb . Append ( $ "GetOrSetExecuteQuery<{ typeof ( T ) . Name } >: [ \n \t Sql = { sql } ,") ;
135-
136- if ( parameters != null && parameters . Any ( ) )
137- {
138- sb . Append ( $ "\n \t Parameters = { string . Join ( ", " , parameters . Select ( x => x . ToString ( ) ) . ToArray ( ) ) } ,") ;
139- }
140-
141- sb . Append ( $ "\n \t CommandType = { cmdType } ]") ;
142-
143- var key = sb . ToString ( ) ;
141+ var key = FormatGetOrSetExecuteQueryKey < T > ( sql , cmdType , parameters ) ;
144142
145143 return cacheProvider . GetOrSet < int > ( key , getter , logger ) ;
146144 }
147-
145+
148146 public static QueryResult < T > GetOrSet < T > ( this ICacheProvider cacheProvider , object [ ] keys , IFetchQueryStrategy < T > fetchStrategy , Func < QueryResult < T > > getter , ILogger logger )
149147 {
150148 if ( cacheProvider == null )
@@ -153,8 +151,7 @@ public static QueryResult<T> GetOrSet<T>(this ICacheProvider cacheProvider, obje
153151 if ( keys == null )
154152 throw new ArgumentNullException ( nameof ( keys ) ) ;
155153
156- var f = fetchStrategy != null ? fetchStrategy . ToString ( ) : $ "FetchQueryStrategy<{ typeof ( T ) . Name } >: [ null ]";
157- var key = $ "GetOrSet<{ typeof ( T ) . Name } >: [ \n \t Keys = { string . Join ( ", " , keys . Select ( x => x . ToString ( ) ) . ToArray ( ) ) } ,\n \t { f } ]";
154+ var key = FormatGetOrSetKey < T > ( keys , fetchStrategy ) ;
158155
159156 return cacheProvider . GetOrSet < T > ( key , getter , logger ) ;
160157 }
@@ -170,7 +167,7 @@ public static QueryResult<TResult> GetOrSet<T, TResult>(this ICacheProvider cach
170167 if ( selector == null )
171168 throw new ArgumentNullException ( nameof ( selector ) ) ;
172169
173- var key = $ "GetOrSet< { typeof ( T ) . Name } >: [ \n \t { options } , \n \t Selector = { ExpressionHelper . TranslateToString ( selector ) } ]" ;
170+ var key = FormatGetOrSetKey < T , TResult > ( options , selector ) ;
174171
175172 return cacheProvider . GetOrSet < TResult > ( key , getter , logger ) ;
176173 }
@@ -183,8 +180,7 @@ public static QueryResult<IEnumerable<TResult>> GetOrSetAll<T, TResult>(this ICa
183180 if ( selector == null )
184181 throw new ArgumentNullException ( nameof ( selector ) ) ;
185182
186- var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
187- var key = $ "GetOrSetAll<{ typeof ( T ) . Name } >: [ \n \t { o } ,\n \t Selector = { ExpressionHelper . TranslateToString ( selector ) } ]";
183+ var key = FormatGetOrSetAllKey < T , TResult > ( options , selector ) ;
188184
189185 return cacheProvider . GetOrSet < IEnumerable < TResult > > ( key , getter , logger ) ;
190186 }
@@ -194,8 +190,7 @@ public static QueryResult<int> GetOrSetCount<T>(this ICacheProvider cacheProvide
194190 if ( cacheProvider == null )
195191 throw new ArgumentNullException ( nameof ( cacheProvider ) ) ;
196192
197- var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
198- var key = $ "GetOrSetCount<{ typeof ( T ) . Name } >: [ \n \t { o } ]";
193+ var key = FormatGetOrSetCountKey < T > ( options ) ;
199194
200195 return cacheProvider . GetOrSet < int > ( key , getter , logger ) ;
201196 }
@@ -211,12 +206,11 @@ public static QueryResult<Dictionary<TDictionaryKey, TElement>> GetOrSetDictiona
211206 if ( elementSelector == null )
212207 throw new ArgumentNullException ( nameof ( elementSelector ) ) ;
213208
214- var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
215- var key = $ "GetOrSetDictionary<{ typeof ( T ) . Name } , { typeof ( TDictionaryKey ) . Name } , { typeof ( TElement ) . Name } >: [ \n \t { o } ,\n \t KeySelector = { ExpressionHelper . TranslateToString ( keySelector ) } ,\n \t ElementSelector = { ExpressionHelper . TranslateToString ( elementSelector ) } ]";
209+ var key = FormatGetOrSetDictionaryKey < T , TDictionaryKey , TElement > ( options , keySelector , elementSelector ) ;
216210
217211 return cacheProvider . GetOrSet < Dictionary < TDictionaryKey , TElement > > ( key , getter , logger ) ;
218212 }
219-
213+
220214 public static QueryResult < IEnumerable < TResult > > GetOrSetGroup < T , TGroupKey , TResult > ( this ICacheProvider cacheProvider , IQueryOptions < T > options , Expression < Func < T , TGroupKey > > keySelector , Expression < Func < TGroupKey , IEnumerable < T > , TResult > > resultSelector , Func < QueryResult < IEnumerable < TResult > > > getter , ILogger logger )
221215 {
222216 if ( cacheProvider == null )
@@ -228,8 +222,7 @@ public static QueryResult<IEnumerable<TResult>> GetOrSetGroup<T, TGroupKey, TRes
228222 if ( resultSelector == null )
229223 throw new ArgumentNullException ( nameof ( resultSelector ) ) ;
230224
231- var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
232- var key = $ "GetOrSetGroup<{ typeof ( T ) . Name } , { typeof ( TGroupKey ) . Name } , { typeof ( TResult ) . Name } >: [ \n \t { o } ,\n \t KeySelector = { ExpressionHelper . TranslateToString ( keySelector ) } ,\n \t ResultSelector = { ExpressionHelper . TranslateToString ( resultSelector ) } ]";
225+ var key = FormatGetOrSetGroupKey < T , TGroupKey , TResult > ( options , keySelector , resultSelector ) ;
233226
234227 return cacheProvider . GetOrSet < IEnumerable < TResult > > ( key , getter , logger ) ;
235228 }
@@ -251,7 +244,7 @@ private static async Task<QueryResult<T>> GetOrSetAsync<T>(this ICacheProvider c
251244 if ( cacheExpiration == null )
252245 throw new ArgumentNullException ( nameof ( cacheExpiration ) ) ;
253246
254- var hashedKey = $ " { CacheProviderManager . CachePrefix } _ { CacheProviderManager . GlobalCachingPrefixCounter } _ { key . ToSHA256 ( ) } " ;
247+ var hashedKey = FormatHashKey ( key ) ;
255248
256249 if ( ! cacheProvider . TryGetValue < QueryResult < T > > ( hashedKey , out var value ) )
257250 {
@@ -260,16 +253,7 @@ private static async Task<QueryResult<T>> GetOrSetAsync<T>(this ICacheProvider c
260253 if ( value == null )
261254 return default ( QueryResult < T > ) ;
262255
263- logger . Debug ( cacheExpiration != TimeSpan . Zero
264- ? $ "Setting up cache for { hashedKey } expire handling in { cacheExpiration . TotalSeconds } seconds"
265- : $ "Setting up cache for { hashedKey } ") ;
266-
267- cacheProvider . Cache . Set (
268- hashedKey ,
269- value ,
270- priority ,
271- cacheExpiration ,
272- reason => logger . Debug ( $ "Cache for { hashedKey } has expired. Evicting from cache for { reason } ") ) ;
256+ cacheProvider . SetValue ( hashedKey , key , value , priority , cacheExpiration , logger ) ;
273257 }
274258 else
275259 {
@@ -300,18 +284,7 @@ public static Task<QueryResult<IEnumerable<T>>> GetOrSetExecuteQueryAsync<T>(thi
300284 if ( projector == null )
301285 throw new ArgumentNullException ( nameof ( projector ) ) ;
302286
303- var sb = new StringBuilder ( ) ;
304-
305- sb . Append ( $ "GetOrSetExecuteQueryAsync<{ typeof ( T ) . Name } >: [ \n \t Sql = { sql } ,") ;
306-
307- if ( parameters != null && parameters . Any ( ) )
308- {
309- sb . Append ( $ "\n \t Parameters = { string . Join ( ", " , parameters . Select ( x => x . ToString ( ) ) . ToArray ( ) ) } ,") ;
310- }
311-
312- sb . Append ( $ "\n \t CommandType = { cmdType } ]") ;
313-
314- var key = sb . ToString ( ) ;
287+ var key = FormatGetOrSetExecuteQueryKey < T > ( sql , cmdType , parameters ) ;
315288
316289 return cacheProvider . GetOrSetAsync < IEnumerable < T > > ( key , getter , logger ) ;
317290 }
@@ -324,18 +297,7 @@ public static Task<QueryResult<int>> GetOrSetExecuteQueryAsync<T>(this ICachePro
324297 if ( sql == null )
325298 throw new ArgumentNullException ( nameof ( sql ) ) ;
326299
327- var sb = new StringBuilder ( ) ;
328-
329- sb . Append ( $ "GetOrSetExecuteQueryAsync<{ typeof ( T ) . Name } >: [ \n \t Sql = { sql } ,") ;
330-
331- if ( parameters != null && parameters . Any ( ) )
332- {
333- sb . Append ( $ "\n \t Parameters = { string . Join ( ", " , parameters . Select ( x => x . ToString ( ) ) . ToArray ( ) ) } ,") ;
334- }
335-
336- sb . Append ( $ "\n \t CommandType = { cmdType } ]") ;
337-
338- var key = sb . ToString ( ) ;
300+ var key = FormatGetOrSetExecuteQueryKey < T > ( sql , cmdType , parameters ) ;
339301
340302 return cacheProvider . GetOrSetAsync < int > ( key , getter , logger ) ;
341303 }
@@ -348,8 +310,7 @@ public static Task<QueryResult<T>> GetOrSetAsync<T>(this ICacheProvider cachePro
348310 if ( keys == null )
349311 throw new ArgumentNullException ( nameof ( keys ) ) ;
350312
351- var f = fetchStrategy != null ? fetchStrategy . ToString ( ) : $ "FetchQueryStrategy<{ typeof ( T ) . Name } >: [ null ]";
352- var key = $ "GetOrSetAsync<{ typeof ( T ) . Name } >: [ \n \t Keys = { string . Join ( ", " , keys . Select ( x => x . ToString ( ) ) . ToArray ( ) ) } ,\n \t { f } ]";
313+ var key = FormatGetOrSetKey < T > ( keys , fetchStrategy ) ;
353314
354315 return cacheProvider . GetOrSetAsync < T > ( key , getter , logger ) ;
355316 }
@@ -365,7 +326,7 @@ public static Task<QueryResult<TResult>> GetOrSetAsync<T, TResult>(this ICachePr
365326 if ( selector == null )
366327 throw new ArgumentNullException ( nameof ( selector ) ) ;
367328
368- var key = $ "GetOrSetAsync< { typeof ( T ) . Name } >: [ \n \t { options } , \n \t Selector = { ExpressionHelper . TranslateToString ( selector ) } ]" ;
329+ var key = FormatGetOrSetKey < T , TResult > ( options , selector ) ;
369330
370331 return cacheProvider . GetOrSetAsync < TResult > ( key , getter , logger ) ;
371332 }
@@ -378,8 +339,7 @@ public static Task<QueryResult<IEnumerable<TResult>>> GetOrSetAllAsync<T, TResul
378339 if ( selector == null )
379340 throw new ArgumentNullException ( nameof ( selector ) ) ;
380341
381- var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
382- var key = $ "GetOrSetAllAsync<{ typeof ( T ) . Name } >: [ \n \t { o } ,\n \t Selector = { ExpressionHelper . TranslateToString ( selector ) } ]";
342+ var key = FormatGetOrSetAllKey < T , TResult > ( options , selector ) ;
383343
384344 return cacheProvider . GetOrSetAsync < IEnumerable < TResult > > ( key , getter , logger ) ;
385345 }
@@ -389,8 +349,7 @@ public static Task<QueryResult<int>> GetOrSetCountAsync<T>(this ICacheProvider c
389349 if ( cacheProvider == null )
390350 throw new ArgumentNullException ( nameof ( cacheProvider ) ) ;
391351
392- var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
393- var key = $ "GetOrSetCountAsync<{ typeof ( T ) . Name } >: [ \n \t { o } ]";
352+ var key = FormatGetOrSetCountKey < T > ( options ) ;
394353
395354 return cacheProvider . GetOrSetAsync < int > ( key , getter , logger ) ;
396355 }
@@ -406,8 +365,7 @@ public static Task<QueryResult<Dictionary<TDictionaryKey, TElement>>> GetOrSetDi
406365 if ( elementSelector == null )
407366 throw new ArgumentNullException ( nameof ( elementSelector ) ) ;
408367
409- var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
410- var key = $ "GetOrSetDictionaryAsync<{ typeof ( T ) . Name } , { typeof ( TDictionaryKey ) . Name } , { typeof ( TElement ) . Name } >: [ \n \t { o } ,\n \t KeySelector = { ExpressionHelper . TranslateToString ( keySelector ) } ,\n \t ElementSelector = { ExpressionHelper . TranslateToString ( elementSelector ) } ]";
368+ var key = FormatGetOrSetDictionaryKey < T , TDictionaryKey , TElement > ( options , keySelector , elementSelector ) ;
411369
412370 return cacheProvider . GetOrSetAsync < Dictionary < TDictionaryKey , TElement > > ( key , getter , logger ) ;
413371 }
@@ -423,10 +381,70 @@ public static Task<QueryResult<IEnumerable<TResult>>> GetOrSetGroupAsync<T, TGro
423381 if ( resultSelector == null )
424382 throw new ArgumentNullException ( nameof ( resultSelector ) ) ;
425383
426- var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
427- var key = $ "GetOrSetGroupAsync<{ typeof ( T ) . Name } , { typeof ( TGroupKey ) . Name } , { typeof ( TResult ) . Name } >: [ \n \t { o } ,\n \t KeySelector = { ExpressionHelper . TranslateToString ( keySelector ) } ,\n \t ResultSelector = { ExpressionHelper . TranslateToString ( resultSelector ) } ]";
384+ var key = FormatGetOrSetGroupKey < T , TGroupKey , TResult > ( options , keySelector , resultSelector ) ;
428385
429386 return cacheProvider . GetOrSetAsync < IEnumerable < TResult > > ( key , getter , logger ) ;
430387 }
388+
389+ private static string FormatHashKey ( string key )
390+ {
391+ return $ "{ CacheProviderManager . CachePrefix } _{ CacheProviderManager . GlobalCachingPrefixCounter } _{ key . ToSHA256 ( ) } ";
392+ }
393+
394+ private static string FormatGetOrSetExecuteQueryKey < T > ( string sql , CommandType cmdType , object [ ] parameters )
395+ {
396+ var sb = new StringBuilder ( ) ;
397+
398+ sb . Append ( $ "GetOrSetExecuteQuery<{ typeof ( T ) . Name } >: [ \n \t Sql = { sql } ,") ;
399+
400+ if ( parameters != null && parameters . Any ( ) )
401+ {
402+ sb . Append ( $ "\n \t Parameters = { string . Join ( ", " , parameters . Select ( x => x . ToString ( ) ) . ToArray ( ) ) } ,") ;
403+ }
404+
405+ sb . Append ( $ "\n \t CommandType = { cmdType } ]") ;
406+
407+ return sb . ToString ( ) ;
408+ }
409+
410+ private static string FormatGetOrSetKey < T > ( object [ ] keys , IFetchQueryStrategy < T > fetchStrategy )
411+ {
412+ var f = fetchStrategy != null ? fetchStrategy . ToString ( ) : $ "FetchQueryStrategy<{ typeof ( T ) . Name } >: [ null ]";
413+
414+ return $ "GetOrSet<{ typeof ( T ) . Name } >: [ \n \t Keys = { string . Join ( ", " , keys . Select ( x => x . ToString ( ) ) . ToArray ( ) ) } ,\n \t { f } ]";
415+ }
416+
417+ private static string FormatGetOrSetAllKey < T , TResult > ( IQueryOptions < T > options , Expression < Func < T , TResult > > selector )
418+ {
419+ var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
420+
421+ return $ "GetOrSetAll<{ typeof ( T ) . Name } >: [ \n \t { o } ,\n \t Selector = { ExpressionHelper . TranslateToString ( selector ) } ]";
422+ }
423+
424+ private static string FormatGetOrSetKey < T , TResult > ( IQueryOptions < T > options , Expression < Func < T , TResult > > selector )
425+ {
426+ return $ "GetOrSet<{ typeof ( T ) . Name } >: [ \n \t { options } ,\n \t Selector = { ExpressionHelper . TranslateToString ( selector ) } ]";
427+ }
428+
429+ private static string FormatGetOrSetCountKey < T > ( IQueryOptions < T > options )
430+ {
431+ var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
432+
433+ return $ "GetOrSetCount<{ typeof ( T ) . Name } >: [ \n \t { o } ]";
434+ }
435+
436+ private static string FormatGetOrSetDictionaryKey < T , TDictionaryKey , TElement > ( IQueryOptions < T > options , Expression < Func < T , TDictionaryKey > > keySelector , Expression < Func < T , TElement > > elementSelector )
437+ {
438+ var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
439+
440+ return $ "GetOrSetDictionary<{ typeof ( T ) . Name } , { typeof ( TDictionaryKey ) . Name } , { typeof ( TElement ) . Name } >: [ \n \t { o } ,\n \t KeySelector = { ExpressionHelper . TranslateToString ( keySelector ) } ,\n \t ElementSelector = { ExpressionHelper . TranslateToString ( elementSelector ) } ]";
441+ }
442+
443+ private static string FormatGetOrSetGroupKey < T , TGroupKey , TResult > ( IQueryOptions < T > options , Expression < Func < T , TGroupKey > > keySelector , Expression < Func < TGroupKey , IEnumerable < T > , TResult > > resultSelector )
444+ {
445+ var o = options != null ? options . ToString ( ) : $ "QueryOptions<{ typeof ( T ) . Name } >: [ null ]";
446+
447+ return $ "GetOrSetGroup<{ typeof ( T ) . Name } , { typeof ( TGroupKey ) . Name } , { typeof ( TResult ) . Name } >: [ \n \t { o } ,\n \t KeySelector = { ExpressionHelper . TranslateToString ( keySelector ) } ,\n \t ResultSelector = { ExpressionHelper . TranslateToString ( resultSelector ) } ]";
448+ }
431449 }
432450}
0 commit comments