@@ -45,17 +45,16 @@ public async Task<bool> PutAsync(
45
45
ISessionImplementor session , CancellationToken cancellationToken )
46
46
{
47
47
cancellationToken . ThrowIfCancellationRequested ( ) ;
48
- // 6.0 TODO: inline the call.
49
- #pragma warning disable 612
50
- var cached = await ( PutAsync ( key , returnTypes , result , queryParameters . NaturalKeyLookup , session , cancellationToken ) ) . ConfigureAwait ( false ) ;
51
- #pragma warning restore 612
48
+ if ( queryParameters . NaturalKeyLookup && result . Count == 0 )
49
+ return false ;
52
50
53
- if ( cached && key . ResultTransformer ? . AutoDiscoverTypes == true && key . ResultTransformer . AutoDiscoveredAliases != null )
54
- {
55
- await ( Cache . PutAsync ( new QueryAliasesKey ( key ) , key . ResultTransformer . AutoDiscoveredAliases , cancellationToken ) ) . ConfigureAwait ( false ) ;
56
- }
51
+ var ts = session . Factory . Settings . CacheProvider . NextTimestamp ( ) ;
57
52
58
- return cached ;
53
+ Log . Debug ( "caching query results in region: '{0}'; {1}" , _regionName , key ) ;
54
+
55
+ await ( Cache . PutAsync ( key , await ( GetCacheableResultAsync ( returnTypes , session , result , ts , GetAutoDiscoveredAliases ( key ) , cancellationToken ) ) . ConfigureAwait ( false ) , cancellationToken ) ) . ConfigureAwait ( false ) ;
56
+
57
+ return true ;
59
58
}
60
59
61
60
// Since 5.2
@@ -70,7 +69,7 @@ public async Task<bool> PutAsync(QueryKey key, ICacheAssembler[] returnTypes, IL
70
69
71
70
Log . Debug ( "caching query results in region: '{0}'; {1}" , _regionName , key ) ;
72
71
73
- await ( Cache . PutAsync ( key , await ( GetCacheableResultAsync ( returnTypes , session , result , ts , cancellationToken ) ) . ConfigureAwait ( false ) , cancellationToken ) ) . ConfigureAwait ( false ) ;
72
+ await ( Cache . PutAsync ( key , await ( GetCacheableResultAsync ( returnTypes , session , result , ts , null , cancellationToken ) ) . ConfigureAwait ( false ) , cancellationToken ) ) . ConfigureAwait ( false ) ;
74
73
75
74
return true ;
76
75
}
@@ -94,20 +93,32 @@ public async Task<IList> GetAsync(
94
93
95
94
try
96
95
{
97
- // 6.0 TODO: inline the call.
98
- #pragma warning disable 612
99
- var result = await ( GetAsync ( key , returnTypes , queryParameters . NaturalKeyLookup , spaces , session , cancellationToken ) ) . ConfigureAwait ( false ) ;
100
- #pragma warning restore 612
96
+ if ( Log . IsDebugEnabled ( ) )
97
+ Log . Debug ( "checking cached query results in region: '{0}'; {1}" , _regionName , key ) ;
98
+
99
+ var cacheable = ( IList ) await ( Cache . GetAsync ( key , cancellationToken ) ) . ConfigureAwait ( false ) ;
100
+ if ( cacheable == null )
101
+ {
102
+ Log . Debug ( "query results were not found in cache: {0}" , key ) ;
103
+ return null ;
104
+ }
105
+
106
+ var timestamp = ( long ) cacheable [ 0 ] ;
107
+
108
+ if ( Log . IsDebugEnabled ( ) )
109
+ Log . Debug ( "Checking query spaces for up-to-dateness [{0}]" , StringHelper . CollectionToString ( spaces ) ) ;
110
+
111
+ if ( ! queryParameters . NaturalKeyLookup && ! await ( IsUpToDateAsync ( spaces , timestamp , cancellationToken ) ) . ConfigureAwait ( false ) )
112
+ {
113
+ Log . Debug ( "cached query results were not up to date for: {0}" , key ) ;
114
+ return null ;
115
+ }
116
+
117
+ var result = await ( GetResultFromCacheableAsync ( key , returnTypes , queryParameters . NaturalKeyLookup , session , cacheable , cancellationToken ) ) . ConfigureAwait ( false ) ;
101
118
102
119
if ( result != null && key . ResultTransformer ? . AutoDiscoverTypes == true && result . Count > 0 )
103
120
{
104
- var aliasesKey = new QueryAliasesKey ( key ) ;
105
- if ( ! ( await ( Cache . GetAsync ( aliasesKey , cancellationToken ) ) . ConfigureAwait ( false ) is string [ ] aliases ) )
106
- {
107
- // Cannot properly initialize the result transformer, treat it as a cache miss
108
- Log . Debug ( "query aliases were not found in cache: {0}" , aliasesKey ) ;
109
- return null ;
110
- }
121
+ var aliases = ( string [ ] ) cacheable [ 1 ] ;
111
122
key . ResultTransformer . SupplyAutoDiscoveredParameters ( queryParameters . ResultTransformer , aliases ) ;
112
123
}
113
124
@@ -173,13 +184,7 @@ public async Task<bool[]> PutManyAsync(
173
184
var key = keys [ i ] ;
174
185
cached [ i ] = true ;
175
186
cachedKeys . Add ( key ) ;
176
- cachedResults . Add ( await ( GetCacheableResultAsync ( returnTypes [ i ] , session , result , ts , cancellationToken ) ) . ConfigureAwait ( false ) ) ;
177
-
178
- if ( key . ResultTransformer ? . AutoDiscoverTypes == true && key . ResultTransformer . AutoDiscoveredAliases != null )
179
- {
180
- cachedKeys . Add ( new QueryAliasesKey ( key ) ) ;
181
- cachedResults . Add ( key . ResultTransformer . AutoDiscoveredAliases ) ;
182
- }
187
+ cachedResults . Add ( await ( GetCacheableResultAsync ( returnTypes [ i ] , session , result , ts , GetAutoDiscoveredAliases ( key ) , cancellationToken ) ) . ConfigureAwait ( false ) ) ;
183
188
}
184
189
185
190
await ( _cache . PutManyAsync ( cachedKeys . ToArray ( ) , cachedResults . ToArray ( ) , cancellationToken ) ) . ConfigureAwait ( false ) ;
@@ -238,8 +243,6 @@ public async Task<IList[]> GetManyAsync(
238
243
{
239
244
session . PersistenceContext . BatchFetchQueue . InitializeQueryCacheQueue ( ) ;
240
245
241
- var queryAliasesKeys = new QueryAliasesKey [ keys . Length ] ;
242
- var hasAliasesToFetch = false ;
243
246
for ( var i = 0 ; i < keys . Length ; i ++ )
244
247
{
245
248
var cacheable = ( IList ) cacheables [ i ] ;
@@ -264,35 +267,10 @@ public async Task<IList[]> GetManyAsync(
264
267
finalReturnTypes [ i ] = GetReturnTypes ( key , returnTypes [ i ] , cacheable ) ;
265
268
await ( PerformBeforeAssembleAsync ( finalReturnTypes [ i ] , session , cacheable , cancellationToken ) ) . ConfigureAwait ( false ) ;
266
269
267
- if ( key . ResultTransformer ? . AutoDiscoverTypes == true && cacheable . Count > 0 )
270
+ if ( key . ResultTransformer ? . AutoDiscoverTypes == true && cacheable . Count > 2 )
268
271
{
269
- queryAliasesKeys [ i ] = new QueryAliasesKey ( key ) ;
270
- hasAliasesToFetch = true ;
271
- }
272
- }
273
-
274
- if ( hasAliasesToFetch )
275
- {
276
- var allAliases = await ( _cache . GetManyAsync ( queryAliasesKeys . Where ( k => k != null ) . ToArray ( ) , cancellationToken ) ) . ConfigureAwait ( false ) ;
277
-
278
- var aliasesIndex = 0 ;
279
- for ( var i = 0 ; i < keys . Length ; i ++ )
280
- {
281
- var queryAliasesKey = queryAliasesKeys [ i ] ;
282
- if ( queryAliasesKey == null )
283
- continue ;
284
-
285
- if ( allAliases [ aliasesIndex ] is string [ ] aliases )
286
- {
287
- keys [ i ] . ResultTransformer . SupplyAutoDiscoveredParameters ( queryParameters [ i ] . ResultTransformer , aliases ) ;
288
- }
289
- else
290
- {
291
- // Cannot properly initialize the result transformer, treat it as a cache miss
292
- Log . Debug ( "query aliases were not found in cache: {0}" , queryAliasesKey ) ;
293
- finalReturnTypes [ i ] = null ;
294
- }
295
- aliasesIndex ++ ;
272
+ var aliases = ( string [ ] ) cacheable [ 1 ] ;
273
+ key . ResultTransformer . SupplyAutoDiscoveredParameters ( queryParams . ResultTransformer , aliases ) ;
296
274
}
297
275
}
298
276
@@ -356,10 +334,10 @@ private static async Task<List<object>> GetCacheableResultAsync(
356
334
ICacheAssembler [ ] returnTypes ,
357
335
ISessionImplementor session ,
358
336
IList result ,
359
- long ts , CancellationToken cancellationToken )
337
+ long ts , string [ ] aliases , CancellationToken cancellationToken )
360
338
{
361
339
cancellationToken . ThrowIfCancellationRequested ( ) ;
362
- var cacheable = new List < object > ( result . Count + 1 ) { ts } ;
340
+ var cacheable = new List < object > ( result . Count + 2 ) { ts , aliases } ;
363
341
foreach ( var row in result )
364
342
{
365
343
if ( returnTypes . Length == 1 )
@@ -385,18 +363,16 @@ private static async Task PerformBeforeAssembleAsync(
385
363
{
386
364
var returnType = returnTypes [ 0 ] ;
387
365
388
- // Skip first element, it is the timestamp
389
- for ( var i = 1 ; i < cacheable . Count ; i ++ )
366
+ foreach ( var cached in GetResultsEnumerable ( cacheable ) )
390
367
{
391
- await ( returnType . BeforeAssembleAsync ( cacheable [ i ] , session , cancellationToken ) ) . ConfigureAwait ( false ) ;
368
+ await ( returnType . BeforeAssembleAsync ( cached , session , cancellationToken ) ) . ConfigureAwait ( false ) ;
392
369
}
393
370
}
394
371
else
395
372
{
396
- // Skip first element, it is the timestamp
397
- for ( var i = 1 ; i < cacheable . Count ; i ++ )
373
+ foreach ( var cached in GetResultsEnumerable ( cacheable ) )
398
374
{
399
- await ( TypeHelper . BeforeAssembleAsync ( ( object [ ] ) cacheable [ i ] , returnTypes , session , cancellationToken ) ) . ConfigureAwait ( false ) ;
375
+ await ( TypeHelper . BeforeAssembleAsync ( ( object [ ] ) cached , returnTypes , session , cancellationToken ) ) . ConfigureAwait ( false ) ;
400
376
}
401
377
}
402
378
}
@@ -411,15 +387,14 @@ private async Task<IList> PerformAssembleAsync(
411
387
cancellationToken . ThrowIfCancellationRequested ( ) ;
412
388
try
413
389
{
414
- var result = new List < object > ( cacheable . Count - 1 ) ;
390
+ var result = new List < object > ( cacheable . Count - 2 ) ;
415
391
if ( returnTypes . Length == 1 )
416
392
{
417
393
var returnType = returnTypes [ 0 ] ;
418
394
419
- // Skip first element, it is the timestamp
420
- for ( var i = 1 ; i < cacheable . Count ; i ++ )
395
+ foreach ( var cached in GetResultsEnumerable ( cacheable ) )
421
396
{
422
- result . Add ( await ( returnType . AssembleAsync ( cacheable [ i ] , session , null , cancellationToken ) ) . ConfigureAwait ( false ) ) ;
397
+ result . Add ( await ( returnType . AssembleAsync ( cached , session , null , cancellationToken ) ) . ConfigureAwait ( false ) ) ;
423
398
}
424
399
}
425
400
else
@@ -433,10 +408,9 @@ private async Task<IList> PerformAssembleAsync(
433
408
}
434
409
}
435
410
436
- // Skip first element, it is the timestamp
437
- for ( var i = 1 ; i < cacheable . Count ; i ++ )
411
+ foreach ( var cached in GetResultsEnumerable ( cacheable ) )
438
412
{
439
- result . Add ( await ( TypeHelper . AssembleAsync ( ( object [ ] ) cacheable [ i ] , returnTypes , nonCollectionTypeIndexes , session , cancellationToken ) ) . ConfigureAwait ( false ) ) ;
413
+ result . Add ( await ( TypeHelper . AssembleAsync ( ( object [ ] ) cached , returnTypes , nonCollectionTypeIndexes , session , cancellationToken ) ) . ConfigureAwait ( false ) ) ;
440
414
}
441
415
}
442
416
@@ -482,17 +456,18 @@ private static async Task InitializeCollectionsAsync(
482
456
return ;
483
457
}
484
458
485
- // Skip first element, it is the timestamp
486
- for ( var i = 1 ; i < cacheResult . Count ; i ++ )
459
+ var j = 0 ;
460
+ foreach ( var cached in GetResultsEnumerable ( cacheResult ) )
487
461
{
488
462
// Initialization of the fetched collection must be done at the end in order to be able to batch fetch them
489
463
// from the cache or database. The collections were already created when their owners were assembled so we only
490
464
// have to initialize them.
491
465
await ( TypeHelper . InitializeCollectionsAsync (
492
- ( object [ ] ) cacheResult [ i ] ,
493
- ( object [ ] ) assembleResult [ i - 1 ] ,
466
+ ( object [ ] ) cached ,
467
+ ( object [ ] ) assembleResult [ j ] ,
494
468
collectionIndexes ,
495
469
session , cancellationToken ) ) . ConfigureAwait ( false ) ;
470
+ j ++ ;
496
471
}
497
472
}
498
473
0 commit comments