@@ -18,7 +18,7 @@ namespace NHibernate.Cache
18
18
/// </summary>
19
19
public partial class StandardQueryCache : IQueryCache , IBatchableQueryCache
20
20
{
21
- private static readonly INHibernateLogger Log = NHibernateLogger . For ( typeof ( StandardQueryCache ) ) ;
21
+ private static readonly INHibernateLogger Log = NHibernateLogger . For ( typeof ( StandardQueryCache ) ) ;
22
22
private readonly string _regionName ;
23
23
private readonly UpdateTimestampsCache _updateTimestampsCache ;
24
24
private readonly CacheBase _cache ;
@@ -133,7 +133,7 @@ public IList Get(
133
133
return null ;
134
134
}
135
135
136
- var timestamp = ( long ) cacheable [ 0 ] ;
136
+ var timestamp = GetResultsMetadata ( cacheable , out var aliases ) ;
137
137
138
138
if ( Log . IsDebugEnabled ( ) )
139
139
Log . Debug ( "Checking query spaces for up-to-dateness [{0}]" , StringHelper . CollectionToString ( spaces ) ) ;
@@ -148,7 +148,6 @@ public IList Get(
148
148
149
149
if ( result != null && key . ResultTransformer ? . AutoDiscoverTypes == true && result . Count > 0 )
150
150
{
151
- var aliases = ( string [ ] ) cacheable [ 1 ] ;
152
151
key . ResultTransformer . SupplyAutoDiscoveredParameters ( queryParameters . ResultTransformer , aliases ) ;
153
152
}
154
153
@@ -174,7 +173,7 @@ public IList Get(QueryKey key, ICacheAssembler[] returnTypes, bool isNaturalKeyL
174
173
return null ;
175
174
}
176
175
177
- var timestamp = ( long ) cacheable [ 0 ] ;
176
+ var timestamp = GetResultsMetadata ( cacheable , out var _ ) ;
178
177
179
178
if ( Log . IsDebugEnabled ( ) )
180
179
Log . Debug ( "Checking query spaces for up-to-dateness [{0}]" , StringHelper . CollectionToString ( spaces ) ) ;
@@ -256,10 +255,16 @@ public IList[] GetMany(
256
255
257
256
spacesToCheck . Add ( querySpaces ) ;
258
257
checkedSpacesIndexes . Add ( i ) ;
259
- // The timestamp is the first element of the cache result.
260
- checkedSpacesTimestamp . Add ( ( long ) cacheable [ 0 ] ) ;
258
+ var timestamp = GetResultsMetadata ( cacheable , out var aliases ) ;
259
+ checkedSpacesTimestamp . Add ( timestamp ) ;
261
260
if ( Log . IsDebugEnabled ( ) )
262
261
Log . Debug ( "Checking query spaces for up-to-dateness [{0}]" , StringHelper . CollectionToString ( querySpaces ) ) ;
262
+
263
+ var key = keys [ i ] ;
264
+ if ( key . ResultTransformer ? . AutoDiscoverTypes == true && HasResults ( cacheable ) )
265
+ {
266
+ key . ResultTransformer . SupplyAutoDiscoveredParameters ( queryParameters [ i ] . ResultTransformer , aliases ) ;
267
+ }
263
268
}
264
269
265
270
var upToDates = spacesToCheck . Count > 0
@@ -298,12 +303,6 @@ public IList[] GetMany(
298
303
299
304
finalReturnTypes [ i ] = GetReturnTypes ( key , returnTypes [ i ] , cacheable ) ;
300
305
PerformBeforeAssemble ( finalReturnTypes [ i ] , session , cacheable ) ;
301
-
302
- if ( key . ResultTransformer ? . AutoDiscoverTypes == true && cacheable . Count > 2 )
303
- {
304
- var aliases = ( string [ ] ) cacheable [ 1 ] ;
305
- key . ResultTransformer . SupplyAutoDiscoveredParameters ( queryParams . ResultTransformer , aliases ) ;
306
- }
307
306
}
308
307
309
308
for ( var i = 0 ; i < keys . Length ; i ++ )
@@ -374,7 +373,12 @@ private static List<object> GetCacheableResult(
374
373
IList result ,
375
374
long ts , string [ ] aliases )
376
375
{
377
- var cacheable = new List < object > ( result . Count + 2 ) { ts , aliases } ;
376
+ var cacheable =
377
+ new List < object > ( result . Count + 1 )
378
+ {
379
+ aliases == null ? ts : new object [ ] { ts , aliases }
380
+ } ;
381
+
378
382
foreach ( var row in result )
379
383
{
380
384
if ( returnTypes . Length == 1 )
@@ -383,19 +387,37 @@ private static List<object> GetCacheableResult(
383
387
}
384
388
else
385
389
{
386
- cacheable . Add ( TypeHelper . Disassemble ( ( object [ ] ) row , returnTypes , null , session , null ) ) ;
390
+ cacheable . Add ( TypeHelper . Disassemble ( ( object [ ] ) row , returnTypes , null , session , null ) ) ;
387
391
}
388
392
}
389
393
390
394
return cacheable ;
391
395
}
392
396
393
- private static IEnumerable < object > GetResultsEnumerable ( IList results )
397
+ private static long GetResultsMetadata ( IList cacheable , out string [ ] aliases )
398
+ {
399
+ aliases = null ;
400
+
401
+ var metadata = cacheable [ 0 ] ;
402
+ var timestamp = metadata as long ? ;
403
+ if ( timestamp . HasValue )
404
+ return timestamp . Value ;
405
+
406
+ var metadataArray = ( object [ ] ) metadata ;
407
+ aliases = ( string [ ] ) metadataArray [ 1 ] ;
408
+ return ( long ) metadataArray [ 0 ] ;
409
+ }
410
+
411
+ private static bool HasResults ( IList cacheable )
412
+ // First element is the timestamp.
413
+ => cacheable . Count > 1 ;
414
+
415
+ private static IEnumerable < object > GetResultsEnumerable ( IList cacheable )
394
416
{
395
- // Skip first element, it is the timestamp, and the second, it is the aliases .
396
- for ( var i = 2 ; i < results . Count ; i ++ )
417
+ // Skip first element, it is the timestamp.
418
+ for ( var i = 1 ; i < cacheable . Count ; i ++ )
397
419
{
398
- yield return results [ i ] ;
420
+ yield return cacheable [ i ] ;
399
421
}
400
422
}
401
423
@@ -404,7 +426,7 @@ private static ICacheAssembler[] GetReturnTypes(
404
426
ICacheAssembler [ ] returnTypes ,
405
427
IList cacheable )
406
428
{
407
- if ( key . ResultTransformer ? . AutoDiscoverTypes == true && cacheable . Count > 2 )
429
+ if ( key . ResultTransformer ? . AutoDiscoverTypes == true && HasResults ( cacheable ) )
408
430
{
409
431
returnTypes = GuessTypes ( cacheable ) ;
410
432
}
0 commit comments