Skip to content

Commit c9ab94d

Browse files
authored
Fix GetMany for ReadWriteCache
- Combine cahce hit/miss logs where possible Fixes #2127
1 parent c628337 commit c9ab94d

File tree

8 files changed

+99
-195
lines changed

8 files changed

+99
-195
lines changed

src/NHibernate/Async/Cache/NonstrictReadWriteCache.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@ public async Task<object> GetAsync(CacheKey key, long txTimestamp, CancellationT
3232
log.Debug("Cache lookup: {0}", key);
3333
}
3434

35-
object result = await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
36-
if (result != null)
37-
{
38-
log.Debug("Cache hit");
39-
}
40-
else
35+
var result = await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
36+
if (log.IsDebugEnabled())
4137
{
42-
log.Debug("Cache miss");
38+
log.Debug(result != null ? "Cache hit: {0}" : "Cache miss: {0}", key);
4339
}
40+
4441
return result;
4542
}
4643

@@ -51,15 +48,14 @@ public async Task<object[]> GetManyAsync(CacheKey[] keys, long timestamp, Cancel
5148
{
5249
log.Debug("Cache lookup: {0}", string.Join(",", keys.AsEnumerable()));
5350
}
54-
var results = await (_cache.GetManyAsync(keys.Select(o => (object) o).ToArray(), cancellationToken)).ConfigureAwait(false);
55-
if (!log.IsDebugEnabled())
56-
{
57-
return results;
58-
}
59-
for (var i = 0; i < keys.Length; i++)
51+
52+
var results = await (_cache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
53+
if (log.IsDebugEnabled())
6054
{
61-
log.Debug(results[i] != null ? $"Cache hit: {keys[i]}" : $"Cache miss: {keys[i]}");
55+
log.Debug("Cache hit: {0}", string.Join(",", keys.Where((k, i) => results != null)));
56+
log.Debug("Cache miss: {0}", string.Join(",", keys.Where((k, i) => results == null)));
6257
}
58+
6359
return results;
6460
}
6561

src/NHibernate/Async/Cache/ReadOnlyCache.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ public partial class ReadOnlyCache : IBatchableCacheConcurrencyStrategy
2424
public async Task<object> GetAsync(CacheKey key, long timestamp, CancellationToken cancellationToken)
2525
{
2626
cancellationToken.ThrowIfCancellationRequested();
27-
object result = await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
28-
if (result != null && log.IsDebugEnabled())
27+
var result = await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
28+
if (log.IsDebugEnabled())
2929
{
30-
log.Debug("Cache hit: {0}", key);
30+
log.Debug(result != null ? "Cache hit: {0}" : "Cache miss: {0}", key);
3131
}
32-
return result;
32+
33+
return result;
3334
}
3435

3536
public async Task<object[]> GetManyAsync(CacheKey[] keys, long timestamp, CancellationToken cancellationToken)
@@ -39,15 +40,14 @@ public async Task<object[]> GetManyAsync(CacheKey[] keys, long timestamp, Cancel
3940
{
4041
log.Debug("Cache lookup: {0}", string.Join(",", keys.AsEnumerable()));
4142
}
42-
var results = await (_cache.GetManyAsync(keys.Select(o => (object) o).ToArray(), cancellationToken)).ConfigureAwait(false);
43-
if (!log.IsDebugEnabled())
44-
{
45-
return results;
46-
}
47-
for (var i = 0; i < keys.Length; i++)
43+
44+
var results = await (_cache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
45+
if (log.IsDebugEnabled())
4846
{
49-
log.Debug(results[i] != null ? $"Cache hit: {keys[i]}" : $"Cache miss: {keys[i]}");
47+
log.Debug("Cache hit: {0}", string.Join(",", keys.Where((k, i) => results[i] != null)));
48+
log.Debug("Cache miss: {0}", string.Join(",", keys.Where((k, i) => results[i] == null)));
5049
}
50+
5151
return results;
5252
}
5353

@@ -82,7 +82,7 @@ public async Task<bool[]> PutManyAsync(
8282
return result;
8383
}
8484

85-
var checkKeys = new List<CacheKey>();
85+
var checkKeys = new List<object>();
8686
var checkKeyIndexes = new List<int>();
8787
for (var i = 0; i < minimalPuts.Length; i++)
8888
{
@@ -95,7 +95,7 @@ public async Task<bool[]> PutManyAsync(
9595
var skipKeyIndexes = new HashSet<int>();
9696
if (checkKeys.Any())
9797
{
98-
var objects = await (_cache.GetManyAsync(checkKeys.Select(o => (object) o).ToArray(), cancellationToken)).ConfigureAwait(false);
98+
var objects = await (_cache.GetManyAsync(checkKeys.ToArray(), cancellationToken)).ConfigureAwait(false);
9999
for (var i = 0; i < objects.Length; i++)
100100
{
101101
if (objects[i] != null)

src/NHibernate/Async/Cache/ReadWriteCache.cs

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,8 @@ public async Task<object> GetAsync(CacheKey key, long txTimestamp, CancellationT
5353
/*try
5454
{
5555
cache.Lock( key );*/
56-
57-
ILockable lockable = (ILockable) await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
58-
59-
bool gettable = lockable != null && lockable.IsGettable(txTimestamp);
60-
61-
if (gettable)
62-
{
63-
if (log.IsDebugEnabled())
64-
{
65-
log.Debug("Cache hit: {0}", key);
66-
}
67-
68-
return ((CachedItem) lockable).Value;
69-
}
70-
else
71-
{
72-
if (log.IsDebugEnabled())
73-
{
74-
if (lockable == null)
75-
{
76-
log.Debug("Cache miss: {0}", key);
77-
}
78-
else
79-
{
80-
log.Debug("Cached item was locked: {0}", key);
81-
}
82-
}
83-
return null;
84-
}
56+
var lockable = (ILockable) await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
57+
return GetValue(txTimestamp, key, lockable);
8558
/*}
8659
finally
8760
{
@@ -100,27 +73,11 @@ public async Task<object[]> GetManyAsync(CacheKey[] keys, long timestamp, Cancel
10073
var result = new object[keys.Length];
10174
using (await _lockObjectAsync.LockAsync())
10275
{
103-
var lockables = await (_cache.GetManyAsync(keys.Select(o => (object) o).ToArray(), cancellationToken)).ConfigureAwait(false);
76+
var lockables = await (_cache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
10477
for (var i = 0; i < lockables.Length; i++)
10578
{
106-
var lockable = (ILockable) lockables[i];
107-
var gettable = lockable != null && lockable.IsGettable(timestamp);
108-
109-
if (gettable)
110-
{
111-
if (log.IsDebugEnabled())
112-
{
113-
log.Debug("Cache hit: {0}", keys[i]);
114-
}
115-
result[i] = ((CachedItem) lockable).Value;
116-
}
117-
118-
if (log.IsDebugEnabled())
119-
{
120-
log.Debug(lockable == null ? "Cache miss: {0}" : "Cached item was locked: {0}", keys[i]);
121-
}
122-
123-
result[i] = null;
79+
var o = (ILockable) lockables[i];
80+
result[i] = GetValue(timestamp, keys[i], o);
12481
}
12582
}
12683
return result;
@@ -186,12 +143,12 @@ public async Task<bool[]> PutManyAsync(
186143
{
187144
log.Debug("Caching: {0}", string.Join(",", keys.AsEnumerable()));
188145
}
189-
var keysArr = keys.Cast<object>().ToArray();
190-
var lockValue = await (_cache.LockManyAsync(keysArr, cancellationToken)).ConfigureAwait(false);
146+
147+
var lockValue = await (_cache.LockManyAsync(keys, cancellationToken)).ConfigureAwait(false);
191148
try
192149
{
193150
var putBatch = new Dictionary<object, object>();
194-
var lockables = await (_cache.GetManyAsync(keysArr, cancellationToken)).ConfigureAwait(false);
151+
var lockables = await (_cache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
195152
for (var i = 0; i < keys.Length; i++)
196153
{
197154
var key = keys[i];
@@ -212,14 +169,9 @@ public async Task<bool[]> PutManyAsync(
212169
{
213170
if (log.IsDebugEnabled())
214171
{
215-
if (lockable.IsLock)
216-
{
217-
log.Debug("Item was locked: {0}", key);
218-
}
219-
else
220-
{
221-
log.Debug("Item was already cached: {0}", key);
222-
}
172+
log.Debug(
173+
lockable.IsLock ? "Item was locked: {0}" : "Item was already cached: {0}",
174+
key);
223175
}
224176
result[i] = false;
225177
}
@@ -232,7 +184,7 @@ public async Task<bool[]> PutManyAsync(
232184
}
233185
finally
234186
{
235-
await (_cache.UnlockManyAsync(keysArr, lockValue, cancellationToken)).ConfigureAwait(false);
187+
await (_cache.UnlockManyAsync(keys, lockValue, cancellationToken)).ConfigureAwait(false);
236188
}
237189
}
238190
return result;
@@ -283,14 +235,7 @@ public async Task<bool> PutAsync(CacheKey key, object value, long txTimestamp, o
283235
{
284236
if (log.IsDebugEnabled())
285237
{
286-
if (lockable.IsLock)
287-
{
288-
log.Debug("Item was locked: {0}", key);
289-
}
290-
else
291-
{
292-
log.Debug("Item was already cached: {0}", key);
293-
}
238+
log.Debug(lockable.IsLock ? "Item was locked: {0}" : "Item was already cached: {0}", key);
294239
}
295240
return false;
296241
}

src/NHibernate/Async/Cache/StandardQueryCache.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ public async Task<IList[]> GetManyAsync(
173173
if (Log.IsDebugEnabled())
174174
Log.Debug("checking cached query results in region: '{0}'; {1}", _regionName, StringHelper.CollectionToString(keys));
175175

176-
var cacheables = (await (_cache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false)).Cast<IList>().ToArray();
176+
var cacheables = await (_cache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
177177

178178
var spacesToCheck = new List<ISet<string>>();
179179
var checkedSpacesIndexes = new HashSet<int>();
180180
var checkedSpacesTimestamp = new List<long>();
181181
for (var i = 0; i < keys.Length; i++)
182182
{
183-
var cacheable = cacheables[i];
183+
var cacheable = (IList) cacheables[i];
184184
if (cacheable == null)
185185
{
186186
Log.Debug("query results were not found in cache: {0}", keys[i]);
@@ -209,7 +209,7 @@ public async Task<IList[]> GetManyAsync(
209209
var results = new IList[keys.Length];
210210
for (var i = 0; i < keys.Length; i++)
211211
{
212-
var cacheable = cacheables[i];
212+
var cacheable = (IList) cacheables[i];
213213
if (cacheable == null)
214214
continue;
215215

src/NHibernate/Cache/NonstrictReadWriteCache.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,12 @@ public object Get(CacheKey key, long txTimestamp)
5858
log.Debug("Cache lookup: {0}", key);
5959
}
6060

61-
object result = Cache.Get(key);
62-
if (result != null)
63-
{
64-
log.Debug("Cache hit");
65-
}
66-
else
61+
var result = Cache.Get(key);
62+
if (log.IsDebugEnabled())
6763
{
68-
log.Debug("Cache miss");
64+
log.Debug(result != null ? "Cache hit: {0}" : "Cache miss: {0}", key);
6965
}
66+
7067
return result;
7168
}
7269

@@ -76,15 +73,14 @@ public object[] GetMany(CacheKey[] keys, long timestamp)
7673
{
7774
log.Debug("Cache lookup: {0}", string.Join(",", keys.AsEnumerable()));
7875
}
79-
var results = _cache.GetMany(keys.Select(o => (object) o).ToArray());
80-
if (!log.IsDebugEnabled())
81-
{
82-
return results;
83-
}
84-
for (var i = 0; i < keys.Length; i++)
76+
77+
var results = _cache.GetMany(keys);
78+
if (log.IsDebugEnabled())
8579
{
86-
log.Debug(results[i] != null ? $"Cache hit: {keys[i]}" : $"Cache miss: {keys[i]}");
80+
log.Debug("Cache hit: {0}", string.Join(",", keys.Where((k, i) => results != null)));
81+
log.Debug("Cache miss: {0}", string.Join(",", keys.Where((k, i) => results == null)));
8782
}
83+
8884
return results;
8985
}
9086

src/NHibernate/Cache/ReadOnlyCache.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ CacheBase IBatchableCacheConcurrencyStrategy.Cache
4545

4646
public object Get(CacheKey key, long timestamp)
4747
{
48-
object result = Cache.Get(key);
49-
if (result != null && log.IsDebugEnabled())
48+
var result = Cache.Get(key);
49+
if (log.IsDebugEnabled())
5050
{
51-
log.Debug("Cache hit: {0}", key);
51+
log.Debug(result != null ? "Cache hit: {0}" : "Cache miss: {0}", key);
5252
}
53-
return result;
53+
54+
return result;
5455
}
5556

5657
public object[] GetMany(CacheKey[] keys, long timestamp)
@@ -59,15 +60,14 @@ public object[] GetMany(CacheKey[] keys, long timestamp)
5960
{
6061
log.Debug("Cache lookup: {0}", string.Join(",", keys.AsEnumerable()));
6162
}
62-
var results = _cache.GetMany(keys.Select(o => (object) o).ToArray());
63-
if (!log.IsDebugEnabled())
64-
{
65-
return results;
66-
}
67-
for (var i = 0; i < keys.Length; i++)
63+
64+
var results = _cache.GetMany(keys);
65+
if (log.IsDebugEnabled())
6866
{
69-
log.Debug(results[i] != null ? $"Cache hit: {keys[i]}" : $"Cache miss: {keys[i]}");
67+
log.Debug("Cache hit: {0}", string.Join(",", keys.Where((k, i) => results[i] != null)));
68+
log.Debug("Cache miss: {0}", string.Join(",", keys.Where((k, i) => results[i] == null)));
7069
}
70+
7171
return results;
7272
}
7373

@@ -91,7 +91,7 @@ public bool[] PutMany(
9191
return result;
9292
}
9393

94-
var checkKeys = new List<CacheKey>();
94+
var checkKeys = new List<object>();
9595
var checkKeyIndexes = new List<int>();
9696
for (var i = 0; i < minimalPuts.Length; i++)
9797
{
@@ -104,7 +104,7 @@ public bool[] PutMany(
104104
var skipKeyIndexes = new HashSet<int>();
105105
if (checkKeys.Any())
106106
{
107-
var objects = _cache.GetMany(checkKeys.Select(o => (object) o).ToArray());
107+
var objects = _cache.GetMany(checkKeys.ToArray());
108108
for (var i = 0; i < objects.Length; i++)
109109
{
110110
if (objects[i] != null)

0 commit comments

Comments
 (0)