Skip to content

Commit 2a02786

Browse files
committed
Handle cacheable queries
1 parent e509920 commit 2a02786

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/NHibernate/Async/Multi/QueryBatch.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,33 +127,29 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
127127
}
128128

129129
var rowCount = 0;
130+
CacheBatcher cacheBatcher = null;
130131
try
131132
{
132133
if (resultSetsCommand.HasQueries)
133134
{
135+
cacheBatcher = new CacheBatcher(Session);
134136
using (var reader = await (resultSetsCommand.GetReaderAsync(Timeout, cancellationToken)).ConfigureAwait(false))
135137
{
136-
var cacheBatcher = new CacheBatcher(Session);
137138
foreach (var query in _queries)
138139
{
139140
if (query.CachingInformation != null)
140141
{
141-
foreach (var cachingInfo in query.CachingInformation.Where(ci => ci.IsCacheable))
142+
foreach (var cachingInfo in query.CachingInformation)
142143
{
143144
cachingInfo.SetCacheBatcher(cacheBatcher);
144145
}
145146
}
146147

147148
rowCount += await (query.ProcessResultsSetAsync(reader, cancellationToken)).ConfigureAwait(false);
148149
}
149-
await (cacheBatcher.ExecuteBatchAsync(cancellationToken)).ConfigureAwait(false);
150150
}
151151
}
152152

153-
// Query cacheable results must be cached untransformed: the put does not need to wait for
154-
// the ProcessResults.
155-
await (PutCacheableResultsAsync(cancellationToken)).ConfigureAwait(false);
156-
157153
foreach (var query in _queries)
158154
{
159155
//TODO 6.0: Replace with query.ProcessResults();
@@ -162,6 +158,17 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
162158
else
163159
query.ProcessResults();
164160
}
161+
162+
var executeBatchTask = cacheBatcher?.ExecuteBatchAsync(cancellationToken);
163+
164+
if (executeBatchTask != null)
165+
166+
{
167+
168+
await (executeBatchTask).ConfigureAwait(false);
169+
170+
}
171+
await (PutCacheableResultsAsync(cancellationToken)).ConfigureAwait(false);
165172
}
166173
catch (OperationCanceledException) { throw; }
167174
catch (Exception sqle)

src/NHibernate/Async/Multi/QueryBatchItemBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public async Task ProcessResultsAsync(CancellationToken cancellationToken)
121121
cancellationToken.ThrowIfCancellationRequested();
122122
ThrowIfNotInitialized();
123123

124-
await (InitializeEntitiesAndCollectionsAsync(_reader, _hydratedObjects, cancellationToken)).ConfigureAwait(false);
124+
using (Session.SwitchCacheMode(_cacheMode))
125+
await (InitializeEntitiesAndCollectionsAsync(_reader, _hydratedObjects, cancellationToken)).ConfigureAwait(false);
126+
125127
for (var i = 0; i < _queryInfos.Count; i++)
126128
{
127129
var queryInfo = _queryInfos[i];

src/NHibernate/Multi/QueryBatch.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,33 +153,29 @@ protected void ExecuteBatched()
153153
}
154154

155155
var rowCount = 0;
156+
CacheBatcher cacheBatcher = null;
156157
try
157158
{
158159
if (resultSetsCommand.HasQueries)
159160
{
161+
cacheBatcher = new CacheBatcher(Session);
160162
using (var reader = resultSetsCommand.GetReader(Timeout))
161163
{
162-
var cacheBatcher = new CacheBatcher(Session);
163164
foreach (var query in _queries)
164165
{
165166
if (query.CachingInformation != null)
166167
{
167-
foreach (var cachingInfo in query.CachingInformation.Where(ci => ci.IsCacheable))
168+
foreach (var cachingInfo in query.CachingInformation)
168169
{
169170
cachingInfo.SetCacheBatcher(cacheBatcher);
170171
}
171172
}
172173

173174
rowCount += query.ProcessResultsSet(reader);
174175
}
175-
cacheBatcher.ExecuteBatch();
176176
}
177177
}
178178

179-
// Query cacheable results must be cached untransformed: the put does not need to wait for
180-
// the ProcessResults.
181-
PutCacheableResults();
182-
183179
foreach (var query in _queries)
184180
{
185181
//TODO 6.0: Replace with query.ProcessResults();
@@ -188,6 +184,9 @@ protected void ExecuteBatched()
188184
else
189185
query.ProcessResults();
190186
}
187+
188+
cacheBatcher?.ExecuteBatch();
189+
PutCacheableResults();
191190
}
192191
catch (Exception sqle)
193192
{

src/NHibernate/Multi/QueryBatchItemBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ public void ProcessResults()
261261
{
262262
ThrowIfNotInitialized();
263263

264-
InitializeEntitiesAndCollections(_reader, _hydratedObjects);
264+
using (Session.SwitchCacheMode(_cacheMode))
265+
InitializeEntitiesAndCollections(_reader, _hydratedObjects);
266+
265267
for (var i = 0; i < _queryInfos.Count; i++)
266268
{
267269
var queryInfo = _queryInfos[i];

0 commit comments

Comments
 (0)