github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/expires/id_key_map.go (about) 1 // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package expires 4 5 type IdKeyMap struct { 6 idKeys map[int64]string // id => key 7 keyIds map[string]int64 // key => id 8 } 9 10 func NewIdKeyMap() *IdKeyMap { 11 return &IdKeyMap{ 12 idKeys: map[int64]string{}, 13 keyIds: map[string]int64{}, 14 } 15 } 16 17 func (this *IdKeyMap) Add(id int64, key string) { 18 oldKey, ok := this.idKeys[id] 19 if ok { 20 delete(this.keyIds, oldKey) 21 } 22 23 oldId, ok := this.keyIds[key] 24 if ok { 25 delete(this.idKeys, oldId) 26 } 27 28 this.idKeys[id] = key 29 this.keyIds[key] = id 30 } 31 32 func (this *IdKeyMap) Key(id int64) (key string, ok bool) { 33 key, ok = this.idKeys[id] 34 return 35 } 36 37 func (this *IdKeyMap) Id(key string) (id int64, ok bool) { 38 id, ok = this.keyIds[key] 39 return 40 } 41 42 func (this *IdKeyMap) DeleteId(id int64) { 43 key, ok := this.idKeys[id] 44 if ok { 45 delete(this.keyIds, key) 46 } 47 delete(this.idKeys, id) 48 } 49 50 func (this *IdKeyMap) DeleteKey(key string) { 51 id, ok := this.keyIds[key] 52 if ok { 53 delete(this.idKeys, id) 54 } 55 delete(this.keyIds, key) 56 } 57 58 func (this *IdKeyMap) Len() int { 59 return len(this.idKeys) 60 } 61 62 func (this *IdKeyMap) IdKeys() map[int64]string { 63 return this.idKeys 64 } 65 66 func (this *IdKeyMap) KeyIds() map[string]int64 { 67 return this.keyIds 68 }