@@ -1565,8 +1565,79 @@ public async Task QueryFetchEntityBatchCacheTestAsync(bool clearEntityCacheAfter
1565
1565
Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( future ? 2 : 1 ) , "Unexpected cache hit count" ) ;
1566
1566
}
1567
1567
1568
+ [ Test ]
1569
+ public async Task CollectionLazyInitializationFromCacheIsBatchedAsync ( )
1570
+ {
1571
+ using ( var s = OpenSession ( ) )
1572
+ {
1573
+ var readOnly = await ( s . GetAsync < ReadOnly > ( await ( s . Query < ReadOnly > ( ) . Select ( x => x . Id ) . FirstAsync ( ) ) ) ) ;
1574
+ Assert . That ( readOnly . Items . Count , Is . EqualTo ( 6 ) ) ;
1575
+ }
1576
+
1577
+ var itemPersister = Sfi . GetEntityPersister ( typeof ( ReadOnlyItem ) . FullName ) ;
1578
+ var itemCache = ( BatchableCache ) itemPersister . Cache . Cache ;
1579
+ itemCache . ClearStatistics ( ) ;
1580
+
1581
+ using ( var s = OpenSession ( ) )
1582
+ {
1583
+ var readOnly = await ( s . GetAsync < ReadOnly > ( await ( s . Query < ReadOnly > ( ) . Select ( x => x . Id ) . FirstAsync ( ) ) ) ) ;
1584
+ Assert . That ( readOnly . Items . Count , Is . EqualTo ( 6 ) ) ;
1585
+ }
1586
+
1587
+ // 6 items with batch-size = 4 so 2 GetMany calls are expected 1st call: 4 items + 2nd call: 2 items
1588
+ Assert . That ( itemCache . GetMultipleCalls . Count , Is . EqualTo ( 2 ) ) ;
1589
+ }
1590
+
1591
+ [ Test ]
1592
+ public async Task CollectionLazyInitializationFromCacheIsBatched_FillCacheByQueryCacheAsync ( )
1593
+ {
1594
+ var itemPersister = Sfi . GetEntityPersister ( typeof ( ReadOnlyItem ) . FullName ) ;
1595
+ var itemCache = ( BatchableCache ) itemPersister . Cache . Cache ;
1596
+ itemCache . ClearStatistics ( ) ;
1597
+ int id ;
1598
+ using ( var s = OpenSession ( ) )
1599
+ {
1600
+ id = await ( s . Query < ReadOnly > ( ) . Select ( x => x . Id ) . FirstAsync ( ) ) ;
1601
+ var readOnly = ( await ( s . Query < ReadOnly > ( ) . Fetch ( x => x . Items )
1602
+ . Where ( x => x . Id == id )
1603
+ . WithOptions ( x => x . SetCacheable ( true ) )
1604
+ . ToListAsync ( ) ) )
1605
+ . First ( ) ;
1606
+ Assert . That ( itemCache . PutMultipleCalls . Count , Is . EqualTo ( 1 ) ) ;
1607
+ Assert . That ( itemCache . GetMultipleCalls . Count , Is . EqualTo ( 0 ) ) ;
1608
+ Assert . That ( NHibernateUtil . IsInitialized ( readOnly . Items ) ) ;
1609
+ Assert . That ( readOnly . Items . Count , Is . EqualTo ( 6 ) ) ;
1610
+ }
1611
+
1612
+ itemCache . ClearStatistics ( ) ;
1613
+ using ( var s = OpenSession ( ) )
1614
+ {
1615
+ var readOnly = ( await ( s . Query < ReadOnly > ( ) . Fetch ( x => x . Items )
1616
+ . Where ( x => x . Id == id )
1617
+ . WithOptions ( x => x . SetCacheable ( true ) )
1618
+ . ToListAsync ( ) ) )
1619
+ . First ( ) ;
1620
+ Assert . That ( itemCache . PutMultipleCalls . Count , Is . EqualTo ( 0 ) ) ;
1621
+ Assert . That ( itemCache . GetMultipleCalls . Count , Is . EqualTo ( 1 ) ) ;
1622
+ Assert . That ( NHibernateUtil . IsInitialized ( readOnly . Items ) ) ;
1623
+ Assert . That ( readOnly . Items . Count , Is . EqualTo ( 6 ) ) ;
1624
+ }
1625
+
1626
+ itemCache . ClearStatistics ( ) ;
1627
+
1628
+
1629
+ using ( var s = OpenSession ( ) )
1630
+ {
1631
+ var readOnly = await ( s . GetAsync < ReadOnly > ( id ) ) ;
1632
+ Assert . That ( readOnly . Items . Count , Is . EqualTo ( 6 ) ) ;
1633
+ }
1634
+
1635
+ // 6 items with batch-size = 4 so 2 GetMany calls are expected 1st call: 4 items + 2nd call: 2 items
1636
+ Assert . That ( itemCache . GetMultipleCalls . Count , Is . EqualTo ( 2 ) ) ;
1637
+ }
1638
+
1568
1639
private async Task AssertMultipleCacheCallsAsync < TEntity > ( IEnumerable < int > loadIds , IReadOnlyList < int > getIds , int idIndex ,
1569
- int [ ] [ ] fetchedIdIndexes , int [ ] putIdIndexes , Func < int , bool > cacheBeforeLoadFn = null , CancellationToken cancellationToken = default ( CancellationToken ) )
1640
+ int [ ] [ ] fetchedIdIndexes , int [ ] putIdIndexes , Func < int , bool > cacheBeforeLoadFn = null , CancellationToken cancellationToken = default ( CancellationToken ) )
1570
1641
where TEntity : CacheEntity
1571
1642
{
1572
1643
var persister = Sfi . GetEntityPersister ( typeof ( TEntity ) . FullName ) ;
0 commit comments