parent
99b83e377b
commit
b434b89a71
@ -1,59 +0,0 @@
|
||||
package golock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.dtapp.net/library/goredis"
|
||||
"go.dtapp.net/library/gouuid"
|
||||
"time"
|
||||
)
|
||||
|
||||
type lock struct {
|
||||
key string
|
||||
expiration time.Duration
|
||||
requestId string
|
||||
Db goredis.App
|
||||
}
|
||||
|
||||
func NewLock(key string, expiration time.Duration) *lock {
|
||||
requestId := gouuid.GetUuId()
|
||||
return &lock{key: key, expiration: expiration, requestId: requestId}
|
||||
}
|
||||
|
||||
// Get 获取锁
|
||||
func (lk *lock) Get() bool {
|
||||
|
||||
cxt, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
|
||||
defer cancel()
|
||||
|
||||
ok, err := lk.Db.Db.SetNX(cxt, lk.key, lk.requestId, lk.expiration).Result()
|
||||
|
||||
if err != nil {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
// Release 释放锁
|
||||
func (lk *lock) Release() error {
|
||||
|
||||
cxt, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
|
||||
defer cancel()
|
||||
|
||||
const luaScript = `
|
||||
if redis.call('get', KEYS[1])==ARGV[1] then
|
||||
return redis.call('del', KEYS[1])
|
||||
else
|
||||
return 0
|
||||
end
|
||||
`
|
||||
|
||||
script := redis.NewScript(luaScript)
|
||||
|
||||
_, err := script.Run(cxt, lk.Db.Db, []string{lk.key}, lk.requestId).Result()
|
||||
return err
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package golock
|
||||
|
||||
import (
|
||||
"go.dtapp.net/library/goredis"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ConfigLockRedis struct {
|
||||
Key string
|
||||
KeyContent string
|
||||
ExpirationTime time.Duration
|
||||
}
|
||||
|
||||
type LockRedis struct {
|
||||
config ConfigLockRedis
|
||||
db goredis.App
|
||||
}
|
||||
|
||||
func NewLockRedis(db goredis.App) *LockRedis {
|
||||
return &LockRedis{db: db}
|
||||
}
|
||||
|
||||
// Lock 上锁
|
||||
func (lockRedis *LockRedis) Lock() bool {
|
||||
judgeCache := lockRedis.db.NewStringOperation().Get(lockRedis.config.Key).UnwrapOr("")
|
||||
if judgeCache != "" {
|
||||
return true
|
||||
}
|
||||
lockRedis.db.NewStringOperation().Set(lockRedis.config.Key, lockRedis.config.KeyContent, goredis.WithExpire(lockRedis.config.ExpirationTime))
|
||||
return true
|
||||
}
|
||||
|
||||
// Unlock Lock 解锁
|
||||
func (lockRedis *LockRedis) Unlock() {
|
||||
lockRedis.db.NewStringOperation().Del(lockRedis.config.Key)
|
||||
}
|
||||
|
||||
// LockForever 永远上锁
|
||||
func (lockRedis *LockRedis) LockForever() bool {
|
||||
judgeCache := lockRedis.db.NewStringOperation().Get(lockRedis.config.Key).UnwrapOr("")
|
||||
if judgeCache != "" {
|
||||
return true
|
||||
}
|
||||
lockRedis.db.NewStringOperation().Set(lockRedis.config.Key, lockRedis.config.KeyContent)
|
||||
return true
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package go_library
|
||||
|
||||
func Version() string {
|
||||
return "v2022-06-07"
|
||||
return "v2022-06-10"
|
||||
}
|
||||
|
Loading…
Reference in new issue