github.com/ngicks/gokugen@v0.0.5/task_storage/sync_state_store.go (about) 1 package taskstorage 2 3 import "sync" 4 5 type SyncStateStore struct { 6 m *sync.Map 7 } 8 9 func NewSyncStateStore() *SyncStateStore { 10 return &SyncStateStore{ 11 m: new(sync.Map), 12 } 13 } 14 15 func (s *SyncStateStore) Put(v string, state TaskState) { 16 s.m.Store(v, state) 17 } 18 19 func (s *SyncStateStore) Remove(v string) (removed bool) { 20 _, removed = s.m.LoadAndDelete(v) 21 return 22 } 23 24 func (s *SyncStateStore) Has(v string) (has bool) { 25 _, has = s.m.Load(v) 26 return 27 } 28 29 func (s *SyncStateStore) Len() int { 30 var count int 31 s.m.Range(func(key, value any) bool { count++; return true }) 32 return count 33 } 34 35 type TaskStateSet struct { 36 Key string 37 Value TaskState 38 } 39 40 func (s *SyncStateStore) GetAll() []TaskStateSet { 41 sl := make([]TaskStateSet, 0) 42 s.m.Range(func(k, v any) bool { 43 sl = append(sl, TaskStateSet{Key: k.(string), Value: v.(TaskState)}) 44 return true 45 }) 46 return sl 47 } 48 49 func (s *SyncStateStore) Clone() *SyncStateStore { 50 m := new(sync.Map) 51 s.m.Range(func(k, v any) bool { 52 m.Store(k.(string), v.(TaskState)) 53 return true 54 }) 55 return &SyncStateStore{ 56 m: m, 57 } 58 }