@@ -18,20 +18,20 @@ internal class ResourceCache<TEntity> : IResourceCache<TEntity>
18
18
private const string Status = "Status" ;
19
19
20
20
private readonly CompareLogic _compare = new (
21
- new ComparisonConfig
21
+ new ( )
22
22
{
23
23
Caching = true ,
24
24
AutoClearCache = false ,
25
- MembersToIgnore = new List < string > { ResourceVersion , ManagedFields } ,
25
+ MembersToIgnore = new ( ) { ResourceVersion , ManagedFields } ,
26
26
} ) ;
27
27
28
- private readonly IDictionary < string , TEntity > _cache = new ConcurrentDictionary < string , TEntity > ( ) ;
28
+ private readonly ConcurrentDictionary < string , TEntity > _cache = new ( ) ;
29
29
30
30
private readonly ResourceCacheMetrics < TEntity > _metrics ;
31
31
32
32
public ResourceCache ( OperatorSettings settings )
33
33
{
34
- _metrics = new ResourceCacheMetrics < TEntity > ( settings ) ;
34
+ _metrics = new ( settings ) ;
35
35
}
36
36
37
37
public TEntity Get ( string id ) => _cache [ id ] ;
@@ -40,18 +40,24 @@ public TEntity Upsert(TEntity resource, out CacheComparisonResult result)
40
40
{
41
41
result = CompareCache ( resource ) ;
42
42
43
- if ( result == CacheComparisonResult . New )
44
- {
45
- _cache . Add ( resource . Metadata . Uid , resource . DeepClone ( ) ) ;
46
- }
47
- else
43
+ var clone = resource . DeepClone ( ) ;
44
+ _cache . AddOrUpdate ( resource . Metadata . Uid , clone , ( _ , _ ) => clone ) ;
45
+
46
+ _metrics . CachedItemsSize . Set ( _cache . Count ) ;
47
+ _metrics . CachedItemsSummary . Observe ( _cache . Count ) ;
48
+ return resource ;
49
+ }
50
+
51
+ public void Fill ( IEnumerable < TEntity > entities )
52
+ {
53
+ foreach ( var entity in entities )
48
54
{
49
- _cache [ resource . Metadata . Uid ] = resource . DeepClone ( ) ;
55
+ var clone = entity . DeepClone ( ) ;
56
+ _cache . AddOrUpdate ( entity . Metadata . Uid , clone , ( _ , _ ) => clone ) ;
50
57
}
51
58
52
59
_metrics . CachedItemsSize . Set ( _cache . Count ) ;
53
60
_metrics . CachedItemsSummary . Observe ( _cache . Count ) ;
54
- return resource ;
55
61
}
56
62
57
63
public void Remove ( TEntity resource ) => Remove ( resource . Metadata . Uid ) ;
@@ -94,7 +100,7 @@ private CacheComparisonResult CompareCache(TEntity resource)
94
100
95
101
private void Remove ( string resourceUid )
96
102
{
97
- _cache . Remove ( resourceUid ) ;
103
+ _cache . TryRemove ( resourceUid , out _ ) ;
98
104
_metrics . CachedItemsSize . Set ( _cache . Count ) ;
99
105
_metrics . CachedItemsSummary . Observe ( _cache . Count ) ;
100
106
}
0 commit comments