File tree Expand file tree Collapse file tree
images/virtualization-artifact/pkg/controller/vmop/migration/internal/progress Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -17,10 +17,15 @@ limitations under the License.
1717package progress
1818
1919import (
20- "sync"
2120 "time"
2221
2322 "k8s.io/apimachinery/pkg/types"
23+ utilcache "k8s.io/apimachinery/pkg/util/cache"
24+ )
25+
26+ const (
27+ storeMaxSize = 1024
28+ storeTTL = 30 * time .Minute
2429)
2530
2631type State struct {
@@ -35,41 +40,35 @@ type State struct {
3540}
3641
3742type Store struct {
38- mu sync.RWMutex
39- states map [types.UID ]State
43+ cache * utilcache.LRUExpireCache
4044}
4145
4246func NewStore () * Store {
43- return & Store {states : make ( map [types. UID ] State )}
47+ return & Store {cache : utilcache . NewLRUExpireCache ( storeMaxSize )}
4448}
4549
4650func (s * Store ) Load (uid types.UID ) (State , bool ) {
47- s .mu .RLock ()
48- defer s .mu .RUnlock ()
49- state , ok := s .states [uid ]
50- return state , ok
51+ v , ok := s .cache .Get (uid )
52+ if ! ok {
53+ return State {}, false
54+ }
55+ return v .(State ), true
5156}
5257
5358func (s * Store ) Store (uid types.UID , state State ) {
5459 if uid == "" {
5560 return
5661 }
57- s .mu .Lock ()
58- defer s .mu .Unlock ()
59- s .states [uid ] = state
62+ s .cache .Add (uid , state , storeTTL )
6063}
6164
6265func (s * Store ) Delete (uid types.UID ) {
6366 if uid == "" {
6467 return
6568 }
66- s .mu .Lock ()
67- defer s .mu .Unlock ()
68- delete (s .states , uid )
69+ s .cache .Remove (uid )
6970}
7071
7172func (s * Store ) Len () int {
72- s .mu .RLock ()
73- defer s .mu .RUnlock ()
74- return len (s .states )
73+ return len (s .cache .Keys ())
7574}
You can’t perform that action at this time.
0 commit comments