-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlock.go
43 lines (34 loc) · 1.05 KB
/
lock.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
package dcron
import "time"
// UnlockScript use lua for redis use
const UnlockScript = `
if redis.call("get",KEYS[1]) == ARGV[1] then
return redis.call("del",KEYS[1])
else
return 0
end`
const ExpireScript = `if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("PEXPIRE", KEYS[1], ARGV[2])
else
return 0
end`
type LockHasExpired interface {
Lock(key string, value interface{}, expiration time.Duration) (bool, error)
UnLock(key string, value interface{}) (interface{}, error)
ExpireWithVal(key string, value interface{}, expiration time.Duration) (interface{}, error)
TTL(key string) (time.Duration, error)
}
type WithoutLock struct {
}
func (d *WithoutLock) Lock(string, interface{}, time.Duration) (bool, error) {
return true, nil
}
func (d *WithoutLock) UnLock(string, interface{}) (interface{}, error) {
return nil, nil
}
func (d *WithoutLock) ExpireWithVal(key string, value interface{}, expiration time.Duration) (interface{}, error) {
return true, nil
}
func (d *WithoutLock) TTL(string) (time.Duration, error) {
return 0, nil
}