Skip to content

Commit 97d2184

Browse files
authored
Prevent calling UpdateTimestampsCache if query spaces are not changed (#2120)
1 parent e613d8a commit 97d2184

File tree

6 files changed

+31
-63
lines changed

6 files changed

+31
-63
lines changed

src/NHibernate/Async/Cache/StandardQueryCache.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@ protected virtual Task<bool> IsUpToDateAsync(ISet<string> spaces, long timestamp
470470
{
471471
return Task.FromCanceled<bool>(cancellationToken);
472472
}
473+
if (spaces.Count == 0)
474+
return Task.FromResult<bool>(true);
475+
473476
return _updateTimestampsCache.IsUpToDateAsync(spaces, timestamp, cancellationToken);
474477
}
475478
}

src/NHibernate/Async/Cache/UpdateTimestampsCache.cs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,9 @@ private Task SetSpacesTimestampAsync(IReadOnlyCollection<string> spaces, long ts
116116
if (spaces.Count == 0)
117117
return Task.CompletedTask;
118118

119-
var timestamps = new object[spaces.Count];
120-
for (var i = 0; i < timestamps.Length; i++)
121-
{
122-
timestamps[i] = ts;
123-
}
124-
125-
return _updateTimestamps.PutManyAsync(spaces.ToArray(), timestamps, cancellationToken);
119+
return _updateTimestamps.PutManyAsync(
120+
spaces.ToArray<object>(),
121+
ArrayHelper.Fill<object>(ts, spaces.Count), cancellationToken);
126122
}
127123
catch (Exception ex)
128124
{
@@ -139,13 +135,7 @@ public virtual async Task<bool> IsUpToDateAsync(ISet<string> spaces, long timest
139135
if (spaces.Count == 0)
140136
return true;
141137

142-
var keys = new object[spaces.Count];
143-
var index = 0;
144-
foreach (var space in spaces)
145-
{
146-
keys[index++] = space;
147-
}
148-
var lastUpdates = await (_updateTimestamps.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
138+
var lastUpdates = await (_updateTimestamps.GetManyAsync(spaces.ToArray<object>(), cancellationToken)).ConfigureAwait(false);
149139
return lastUpdates.All(lastUpdate => !IsOutdated(lastUpdate as long?, timestamp));
150140
}
151141
}
@@ -156,36 +146,27 @@ public virtual async Task<bool[]> AreUpToDateAsync(ISet<string>[] spaces, long[]
156146
cancellationToken.ThrowIfCancellationRequested();
157147
using (await _areUpToDate.LockAsync())
158148
{
159-
var results = new bool[spaces.Length];
149+
if (spaces.Length == 0)
150+
return Array.Empty<bool>();
151+
160152
var allSpaces = new HashSet<string>();
161153
foreach (var sp in spaces)
162154
{
163155
allSpaces.UnionWith(sp);
164156
}
165157

166158
if (allSpaces.Count == 0)
167-
{
168-
for (var i = 0; i < spaces.Length; i++)
169-
{
170-
results[i] = true;
171-
}
159+
return ArrayHelper.Fill(true, spaces.Length);
172160

173-
return results;
174-
}
161+
var keys = allSpaces.ToArray<object>();
175162

176-
var keys = new object[allSpaces.Count];
177163
var index = 0;
178-
foreach (var space in allSpaces)
179-
{
180-
keys[index++] = space;
181-
}
182-
183-
index = 0;
184164
var lastUpdatesBySpace =
185165
(await (_updateTimestamps
186166
.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false))
187167
.ToDictionary(u => keys[index++], u => u as long?);
188168

169+
var results = new bool[spaces.Length];
189170
for (var i = 0; i < spaces.Length; i++)
190171
{
191172
var timestamp = timestamps[i];

src/NHibernate/Async/Engine/ActionQueue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private Task PreInvalidateCachesAsync(CancellationToken cancellationToken)
4545
{
4646
return Task.FromCanceled<object>(cancellationToken);
4747
}
48-
if (session.Factory.Settings.IsQueryCacheEnabled)
48+
if (session.Factory.Settings.IsQueryCacheEnabled && executedSpaces.Count > 0)
4949
{
5050
return session.Factory.UpdateTimestampsCache.PreInvalidateAsync(executedSpaces, cancellationToken);
5151
}
@@ -165,7 +165,7 @@ public async Task AfterTransactionCompletionAsync(bool success, CancellationToke
165165
private async Task InvalidateCachesAsync(CancellationToken cancellationToken)
166166
{
167167
cancellationToken.ThrowIfCancellationRequested();
168-
if (session.Factory.Settings.IsQueryCacheEnabled)
168+
if (session.Factory.Settings.IsQueryCacheEnabled && executedSpaces.Count > 0)
169169
{
170170
await (session.Factory.UpdateTimestampsCache.InvalidateAsync(executedSpaces, cancellationToken)).ConfigureAwait(false);
171171
}

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ private static ICacheAssembler[] GuessTypes(IList cacheable)
551551

552552
protected virtual bool IsUpToDate(ISet<string> spaces, long timestamp)
553553
{
554+
if (spaces.Count == 0)
555+
return true;
556+
554557
return _updateTimestampsCache.IsUpToDate(spaces, timestamp);
555558
}
556559
}

src/NHibernate/Cache/UpdateTimestampsCache.cs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,9 @@ private void SetSpacesTimestamp(IReadOnlyCollection<string> spaces, long ts)
8888
if (spaces.Count == 0)
8989
return;
9090

91-
var timestamps = new object[spaces.Count];
92-
for (var i = 0; i < timestamps.Length; i++)
93-
{
94-
timestamps[i] = ts;
95-
}
96-
97-
_updateTimestamps.PutMany(spaces.ToArray(), timestamps);
91+
_updateTimestamps.PutMany(
92+
spaces.ToArray<object>(),
93+
ArrayHelper.Fill<object>(ts, spaces.Count));
9894
}
9995

10096
[MethodImpl(MethodImplOptions.Synchronized)]
@@ -103,49 +99,34 @@ public virtual bool IsUpToDate(ISet<string> spaces, long timestamp /* H2.1 has L
10399
if (spaces.Count == 0)
104100
return true;
105101

106-
var keys = new object[spaces.Count];
107-
var index = 0;
108-
foreach (var space in spaces)
109-
{
110-
keys[index++] = space;
111-
}
112-
var lastUpdates = _updateTimestamps.GetMany(keys);
102+
var lastUpdates = _updateTimestamps.GetMany(spaces.ToArray<object>());
113103
return lastUpdates.All(lastUpdate => !IsOutdated(lastUpdate as long?, timestamp));
114104
}
115105

116106
[MethodImpl(MethodImplOptions.Synchronized)]
117107
public virtual bool[] AreUpToDate(ISet<string>[] spaces, long[] timestamps)
118108
{
119-
var results = new bool[spaces.Length];
109+
if (spaces.Length == 0)
110+
return Array.Empty<bool>();
111+
120112
var allSpaces = new HashSet<string>();
121113
foreach (var sp in spaces)
122114
{
123115
allSpaces.UnionWith(sp);
124116
}
125117

126118
if (allSpaces.Count == 0)
127-
{
128-
for (var i = 0; i < spaces.Length; i++)
129-
{
130-
results[i] = true;
131-
}
119+
return ArrayHelper.Fill(true, spaces.Length);
132120

133-
return results;
134-
}
121+
var keys = allSpaces.ToArray<object>();
135122

136-
var keys = new object[allSpaces.Count];
137123
var index = 0;
138-
foreach (var space in allSpaces)
139-
{
140-
keys[index++] = space;
141-
}
142-
143-
index = 0;
144124
var lastUpdatesBySpace =
145125
_updateTimestamps
146126
.GetMany(keys)
147127
.ToDictionary(u => keys[index++], u => u as long?);
148128

129+
var results = new bool[spaces.Length];
149130
for (var i = 0; i < spaces.Length; i++)
150131
{
151132
var timestamp = timestamps[i];
@@ -163,7 +144,7 @@ public virtual void Destroy()
163144
// not the responsibility of this class.
164145
}
165146

166-
private bool IsOutdated(long? lastUpdate, long timestamp)
147+
private static bool IsOutdated(long? lastUpdate, long timestamp)
167148
{
168149
if (!lastUpdate.HasValue)
169150
{

src/NHibernate/Engine/ActionQueue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private void ExecuteActions<T>(List<T> list) where T: IExecutable
172172

173173
private void PreInvalidateCaches()
174174
{
175-
if (session.Factory.Settings.IsQueryCacheEnabled)
175+
if (session.Factory.Settings.IsQueryCacheEnabled && executedSpaces.Count > 0)
176176
{
177177
session.Factory.UpdateTimestampsCache.PreInvalidate(executedSpaces);
178178
}
@@ -294,7 +294,7 @@ public void AfterTransactionCompletion(bool success)
294294

295295
private void InvalidateCaches()
296296
{
297-
if (session.Factory.Settings.IsQueryCacheEnabled)
297+
if (session.Factory.Settings.IsQueryCacheEnabled && executedSpaces.Count > 0)
298298
{
299299
session.Factory.UpdateTimestampsCache.Invalidate(executedSpaces);
300300
}

0 commit comments

Comments
 (0)