This repository was archived by the owner on Mar 13, 2025. It is now read-only.
File tree 1 file changed +12
-0
lines changed
1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package cache
2
2
3
3
import (
4
4
"fmt"
5
+ "sync"
5
6
"time"
6
7
7
8
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -14,15 +15,20 @@ type cacheItem struct {
14
15
15
16
type ResourceCache struct {
16
17
resources map [string ]cacheItem
18
+ lock * sync.RWMutex
17
19
}
18
20
19
21
func NewCache () * ResourceCache {
20
22
return & ResourceCache {
21
23
resources : map [string ]cacheItem {},
24
+ lock : & sync.RWMutex {},
22
25
}
23
26
}
24
27
25
28
func (rc * ResourceCache ) Get (obj * unstructured.Unstructured ) (* unstructured.Unstructured , time.Time ) {
29
+ rc .lock .RLock ()
30
+ defer rc .lock .RUnlock ()
31
+
26
32
existing , exists := rc .resources [rc .objectKey (obj )]
27
33
if ! exists {
28
34
return nil , time.Time {}
@@ -32,13 +38,19 @@ func (rc *ResourceCache) Get(obj *unstructured.Unstructured) (*unstructured.Unst
32
38
}
33
39
34
40
func (rc * ResourceCache ) Set (obj * unstructured.Unstructured ) {
41
+ rc .lock .Lock ()
42
+ defer rc .lock .Unlock ()
43
+
35
44
rc .resources [rc .objectKey (obj )] = cacheItem {
36
45
resource : obj .DeepCopy (),
37
46
lastSeen : time .Now (),
38
47
}
39
48
}
40
49
41
50
func (rc * ResourceCache ) Delete (obj * unstructured.Unstructured ) {
51
+ rc .lock .Lock ()
52
+ defer rc .lock .Unlock ()
53
+
42
54
delete (rc .resources , rc .objectKey (obj ))
43
55
}
44
56
You can’t perform that action at this time.
0 commit comments