@@ -9,7 +9,7 @@ public class Cache<TObject, TKey> : ICache<TObject, TKey>
9
9
public static readonly IReadOnlyCache < TObject , TKey > Empty = new Cache < TObject , TKey > ( _ => default ! ) ;
10
10
11
11
private readonly Func < TObject , TKey > _keySelector ;
12
- private Dictionary < TKey , TObject > _dict = new ( ) ;
12
+ private readonly Dictionary < TKey , TObject > _dict ;
13
13
14
14
public TObject this [ TKey key ] => _dict [ key ] ;
15
15
@@ -21,8 +21,9 @@ public class Cache<TObject, TKey> : ICache<TObject, TKey>
21
21
22
22
public IEnumerable < KeyValuePair < TKey , TObject > > KeyValues => _dict ;
23
23
24
- public Cache ( Func < TObject , TKey > keySelector )
24
+ public Cache ( Func < TObject , TKey > keySelector , IEqualityComparer < TKey > ? equalityComparer = null )
25
25
{
26
+ _dict = new Dictionary < TKey , TObject > ( equalityComparer ) ;
26
27
_keySelector = keySelector ?? throw new ArgumentNullException ( nameof ( keySelector ) ) ;
27
28
}
28
29
@@ -51,40 +52,6 @@ public void Remove(IEnumerable<TKey> keys)
51
52
52
53
return default ;
53
54
}
54
-
55
- public void Refresh ( )
56
- {
57
- var tmp = _dict ;
58
- _dict = new Dictionary < TKey , TObject > ( ) ;
59
- foreach ( var item in tmp . Values )
60
- {
61
- _dict [ _keySelector ( item ) ] = item ;
62
- }
63
- }
64
-
65
- public void Refresh ( IEnumerable < TKey > keys )
66
- {
67
- List < TObject > objs = new ( ) ;
68
- foreach ( var key in keys )
69
- {
70
- if ( _dict . TryGetValue ( key , out var val ) )
71
- {
72
- objs . Add ( val ) ;
73
- _dict . Remove ( key ) ;
74
- }
75
- }
76
- foreach ( var obj in objs )
77
- {
78
- _dict [ _keySelector ( obj ) ] = obj ;
79
- }
80
- }
81
-
82
- public void Refresh ( TKey key )
83
- {
84
- if ( ! TryGetValue ( key , out var val ) ) return ;
85
- _dict . Remove ( key ) ;
86
- _dict [ _keySelector ( val ) ] = val ;
87
- }
88
55
89
56
public IEnumerator < IKeyValue < TKey , TObject > > GetEnumerator ( )
90
57
{
0 commit comments