mirror of https://github.com/q191201771/naza
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
615 B
Go
39 lines
615 B
Go
// package unique 对象唯一ID
|
|
package unique
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
var global Unique
|
|
|
|
func GenUniqueKey(prefix string) string {
|
|
return global.GenUniqueKey(prefix)
|
|
}
|
|
|
|
type Unique struct {
|
|
//id uint64
|
|
|
|
m sync.Mutex
|
|
prefix2id map[string]uint64
|
|
}
|
|
|
|
func (u *Unique) GenUniqueKey(prefix string) string {
|
|
//return fmt.Sprintf("%s%d", prefix, atomic.AddUint64(&u.id, 1))
|
|
u.m.Lock()
|
|
defer u.m.Unlock()
|
|
id, ok := u.prefix2id[prefix]
|
|
if ok {
|
|
id++
|
|
} else {
|
|
id = 1
|
|
}
|
|
u.prefix2id[prefix] = id
|
|
return fmt.Sprintf("%s%d", prefix, id)
|
|
}
|
|
|
|
func init() {
|
|
global.prefix2id = make(map[string]uint64)
|
|
}
|