Skip to content

Commit 0c2e312

Browse files
committed
ColumnNameCache.cs: Can't think of a reason to store nullable ints in the cache dictionary, since we never store the value null - and for retrieval we can use the return value of TryGetValue().
1 parent 90873fb commit 0c2e312

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/NHibernate/AdoNet/ColumnNameCache.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,53 @@
33

44
namespace NHibernate.AdoNet
55
{
6-
/// <summary> Implementation of ColumnNameCache. </summary>
6+
/// <summary> Implementation of ColumnNameCache. Thread safe. </summary>
77
public class ColumnNameCache
88
{
9-
private readonly ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
10-
private readonly Dictionary<string, int?> columnNameToIndexCache;
9+
private readonly ReaderWriterLockSlim _cacheLock = new ReaderWriterLockSlim();
10+
private readonly Dictionary<string, int> _columnNameToIndexCache;
1111

1212
public ColumnNameCache(int columnCount)
1313
{
1414
// should *not* need to grow beyond the size of the total number of columns in the rs
15-
columnNameToIndexCache = new Dictionary<string, int?>(columnCount);
15+
_columnNameToIndexCache = new Dictionary<string, int>(columnCount);
1616
}
1717

1818
public int GetIndexForColumnName(string columnName, ResultSetWrapper rs)
1919
{
20-
int? cached = Read(columnName);
21-
if (cached.HasValue)
20+
int index;
21+
if (!TryRead(columnName, out index))
2222
{
23-
return cached.Value;
24-
}
25-
else
26-
{
27-
int index = rs.Target.GetOrdinal(columnName);
23+
index = rs.Target.GetOrdinal(columnName);
2824
Insert(columnName, index);
29-
return index;
3025
}
26+
27+
return index;
3128
}
3229

33-
private int? Read(string key)
30+
private bool TryRead(string key, out int value)
3431
{
35-
cacheLock.EnterReadLock();
32+
_cacheLock.EnterReadLock();
3633
try
3734
{
38-
int? value;
39-
columnNameToIndexCache.TryGetValue(key, out value);
40-
return value;
35+
return _columnNameToIndexCache.TryGetValue(key, out value);
4136
}
4237
finally
4338
{
44-
cacheLock.ExitReadLock();
39+
_cacheLock.ExitReadLock();
4540
}
4641
}
4742

4843
private void Insert(string key, int value)
4944
{
50-
cacheLock.EnterWriteLock();
45+
_cacheLock.EnterWriteLock();
5146
try
5247
{
53-
columnNameToIndexCache[key] = value;
48+
_columnNameToIndexCache[key] = value;
5449
}
5550
finally
5651
{
57-
cacheLock.ExitWriteLock();
52+
_cacheLock.ExitWriteLock();
5853
}
5954
}
6055
}

0 commit comments

Comments
 (0)