init: web_hgdjc_server
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package synclock
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"videoweb/pub/common/errors"
|
||||
"videoweb/pub/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
UserExtSyncLockFmt = "userExtSyncLock:%d" // 用户账户扩展信息
|
||||
UserExtSyncLockExpire = 5 * time.Minute // 用户账户扩展信息 过期时间
|
||||
|
||||
UserAccountLockFmt = "UserAccountLockFmt:%d" // 用户账户
|
||||
UserAccountLockExpire = 5 * time.Minute // 用户账户 过期时间
|
||||
TraderAccountLockFmt = "TraderAccountLockFmt:%s" //商人账户
|
||||
SpinLockExpire = 3 * time.Second
|
||||
)
|
||||
|
||||
// UserExtSyncLock 通过redis按照用户ID加锁
|
||||
func UserExtcLock(userId int64) bool {
|
||||
v, e := redis.Setnx_NewOK(fmt.Sprintf(UserExtSyncLockFmt, userId), true, UserExtSyncLockExpire)
|
||||
if !v || e != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// UserExtSpinLock 通过redis按照用户ID加锁
|
||||
func UserExtSpinLock(userId int64, lockTimeOut time.Duration) (bool, error) {
|
||||
endTime := time.Now().Add(lockTimeOut)
|
||||
for time.Now().Before(endTime) {
|
||||
value, err := redis.Setnx_NewOK(fmt.Sprintf(UserExtSyncLockFmt, userId), true, UserExtSyncLockExpire)
|
||||
if err != nil {
|
||||
return false, errors.MakeWithMsg(errors.RedisError, "redis未启动")
|
||||
}
|
||||
if value {
|
||||
return true, nil
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
|
||||
return false, errors.MakeWithMsg(errors.RedisError, "用户操作繁忙")
|
||||
}
|
||||
|
||||
// UserExtUnlock 按照用户ID释放锁
|
||||
func UserExtUnlock(userId int64) {
|
||||
redis.Del(fmt.Sprintf(UserExtSyncLockFmt, userId))
|
||||
}
|
||||
|
||||
// UserAccountLock 通过redis按照用户ID加锁
|
||||
func UserAccountLock(userId int64) bool {
|
||||
v, e := redis.Setnx_NewOK(fmt.Sprintf(UserAccountLockFmt, userId), true, UserExtSyncLockExpire)
|
||||
if !v || e != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// UserAccountSpinLock 通过redis按照用户ID加锁
|
||||
func UserAccountSpinLock(userId int64, lockTimeOut time.Duration) (bool, error) {
|
||||
endTime := time.Now().Add(lockTimeOut)
|
||||
for time.Now().Before(endTime) {
|
||||
value, err := redis.Setnx_NewOK(fmt.Sprintf(UserAccountLockFmt, userId), true, UserExtSyncLockExpire)
|
||||
if err != nil {
|
||||
return false, errors.MakeWithMsg(errors.RedisError, "redis未启动")
|
||||
}
|
||||
if value {
|
||||
return true, nil
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
return false, errors.MakeWithMsg(errors.RedisError, "用户操作繁忙")
|
||||
}
|
||||
|
||||
// 通过redis按照商人ID加锁
|
||||
func TraderAccountSpinLock(userId string, lockTimeOut time.Duration) (bool, error) {
|
||||
endTime := time.Now().Add(lockTimeOut)
|
||||
for time.Now().Before(endTime) {
|
||||
value, err := redis.Setnx_NewOK(fmt.Sprintf(TraderAccountLockFmt, userId), true, SpinLockExpire)
|
||||
if err != nil {
|
||||
return false, errors.MakeWithMsg(errors.RedisError, "redis未启动")
|
||||
}
|
||||
if value {
|
||||
return true, nil
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
return false, errors.MakeWithMsg(errors.RedisError, "用户操作繁忙")
|
||||
}
|
||||
|
||||
// 按照商人ID释放锁
|
||||
func TraderAccountUnlock(userId string) {
|
||||
redis.Del(fmt.Sprintf(TraderAccountLockFmt, userId))
|
||||
}
|
||||
|
||||
// UserAccountUnlock 按照用户ID释放锁
|
||||
func UserAccountUnlock(userId int64) {
|
||||
redis.Del(fmt.Sprintf(UserAccountLockFmt, userId))
|
||||
}
|
||||
Reference in New Issue
Block a user