-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwlock.go
44 lines (36 loc) · 1.19 KB
/
rwlock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package redislock
import (
"context"
"time"
"github.com/WeiJiadong/redislock/internal"
"github.com/go-redis/redis/v9"
)
// RWMutex 读写锁定义
type RWMutex struct {
internal.LockInfo
}
// RWLock 读写锁加写锁
func (rw *RWMutex) WLock(ctx context.Context, client *redis.Client) error {
return internal.ExecLuaScript(ctx, client, rw.Key, internal.WLockScript, rw.Val, rw.Lease/time.Second)
}
// RWLock 读写锁加读锁
func (rw *RWMutex) RLock(ctx context.Context, client *redis.Client) error {
return internal.ExecLuaScript(ctx, client, rw.Key, internal.RLockScript, rw.Val, rw.Lease/time.Second)
}
// Unlock 读写锁解写锁
func (rw *RWMutex) WUnlock(ctx context.Context, client *redis.Client) error {
return internal.ExecLuaScript(ctx, client, rw.Key, internal.WUnlockScript, rw.Val)
}
// Unlock 读写锁解读锁
func (rw *RWMutex) RUnlock(ctx context.Context, client *redis.Client) error {
return internal.ExecLuaScript(ctx, client, rw.Key, internal.RUnlockScript, rw.Val)
}
// NewRWMutex 读写锁构造函数
func NewRWMutex(Key, Val string, Lease time.Duration) *RWMutex {
return &RWMutex{
LockInfo: internal.LockInfo{
Key: Key,
Val: Val,
Lease: Lease,
}}
}