File tree Expand file tree Collapse file tree 1 file changed +32
-3
lines changed Expand file tree Collapse file tree 1 file changed +32
-3
lines changed Original file line number Diff line number Diff line change 1
1
using System . Collections . Generic ;
2
+ using System . Threading ;
2
3
3
4
namespace NHibernate . AdoNet
4
5
{
5
6
/// <summary> Implementation of ColumnNameCache. </summary>
6
7
public class ColumnNameCache
7
8
{
9
+ private readonly ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim ( ) ;
8
10
private readonly Dictionary < string , int ? > columnNameToIndexCache ;
9
11
10
12
public ColumnNameCache ( int columnCount )
@@ -15,18 +17,45 @@ public ColumnNameCache(int columnCount)
15
17
16
18
public int GetIndexForColumnName ( string columnName , ResultSetWrapper rs )
17
19
{
18
- int ? cached ;
19
- columnNameToIndexCache . TryGetValue ( columnName , out cached ) ;
20
+ int ? cached = Read ( columnName ) ;
20
21
if ( cached . HasValue )
21
22
{
22
23
return cached . Value ;
23
24
}
24
25
else
25
26
{
26
27
int index = rs . Target . GetOrdinal ( columnName ) ;
27
- columnNameToIndexCache [ columnName ] = index ;
28
+ Insert ( columnName , index ) ;
28
29
return index ;
29
30
}
30
31
}
32
+
33
+ private int ? Read ( string key )
34
+ {
35
+ cacheLock . EnterReadLock ( ) ;
36
+ try
37
+ {
38
+ int ? value ;
39
+ columnNameToIndexCache . TryGetValue ( key , out value ) ;
40
+ return value ;
41
+ }
42
+ finally
43
+ {
44
+ cacheLock . ExitReadLock ( ) ;
45
+ }
46
+ }
47
+
48
+ private void Insert ( string key , int value )
49
+ {
50
+ cacheLock . EnterWriteLock ( ) ;
51
+ try
52
+ {
53
+ columnNameToIndexCache [ key ] = value ;
54
+ }
55
+ finally
56
+ {
57
+ cacheLock . ExitWriteLock ( ) ;
58
+ }
59
+ }
31
60
}
32
61
}
You can’t perform that action at this time.
0 commit comments