Set sets a new entry or updates an existing one. Set adds or updates an entry in the SyncMap with the specified key. If the key already exists in the map, the entry will be updated. If the key does not exist and the map is at capacity, some entries will be evicted first.
(key string, entry interface{})
| 30 | // If the key already exists in the map, the entry will be updated. |
| 31 | // If the key does not exist and the map is at capacity, some entries will be evicted first. |
| 32 | func (sm *SyncMap) Set(key string, entry interface{}) { |
| 33 | sm.lock.Lock() |
| 34 | defer sm.lock.Unlock() |
| 35 | |
| 36 | if _, ok := (*sm.mapObj)[key]; !ok { |
| 37 | if numEntries := len(*sm.mapObj); numEntries >= sm.capacity { |
| 38 | numToEvict := numEntries * sm.evictionPercentage / 100 |
| 39 | if numToEvict <= 1 { |
| 40 | numToEvict = 1 |
| 41 | } |
| 42 | numEvicted := 0 |
| 43 | for k := range *sm.mapObj { |
| 44 | delete(*sm.mapObj, k) |
| 45 | numEvicted++ |
| 46 | if numEvicted >= numToEvict { |
| 47 | break |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | (*sm.mapObj)[key] = entry |
| 54 | } |
| 55 | |
| 56 | // Delete removes the entry with the specified key from the SyncMap. |
| 57 | // If the key does not exist, this method does nothing. |
no outgoing calls