This commit is contained in:
ljw
2024-09-13 15:57:29 +08:00
commit c53df223d1
112 changed files with 14353 additions and 0 deletions

32
lib/lock/local.go Normal file
View File

@@ -0,0 +1,32 @@
package lock
import (
"sync"
)
type Local struct {
Locks *sync.Map
}
func (l *Local) Lock(key string) {
lock := l.GetLock(key)
lock.Lock()
}
func (l *Local) UnLock(key string) {
lock, ok := l.Locks.Load(key)
if ok {
lock.(*sync.Mutex).Unlock()
}
}
func (l *Local) GetLock(key string) *sync.Mutex {
lock, _ := l.Locks.LoadOrStore(key, &sync.Mutex{})
return lock.(*sync.Mutex)
}
func NewLocal() *Local {
return &Local{
Locks: &sync.Map{},
}
}