forked from xxl-job/xxl-job-executor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
122 lines (101 loc) · 2.04 KB
/
storage.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package xxl
import (
"encoding/json"
"sync"
"time"
)
const (
Persistence = -1
)
type Storager interface {
// Set expireAt 过期时间, 时间戳
Set(taskName, handleName string, jobId, expireAt int64)
Get(taskName string) *Storage
Del(taskName string)
Exists(taskName string) bool
Len() int
GetAll() map[string]*Storage
}
type Storage struct {
HandleName string
ExpireAt int64
JobId int64
}
// Equal 相等
func (s *Storage) Equal(storage *Storage) bool {
marshal1, _ := json.Marshal(s)
marshal2, _ := json.Marshal(storage)
equal := md5V(marshal1) == md5V(marshal2)
return equal
}
// Expired 已过期
func (s *Storage) Expired() bool {
if s == nil {
return true
}
if s.ExpireAt == Persistence {
return false
}
expire := time.Now().Unix() >= s.ExpireAt
return expire
}
// Persistence 是否为永久
func (s *Storage) Persistence() bool {
if s == nil {
return false
}
return s.ExpireAt == Persistence
}
type SessionStorage struct {
mu sync.RWMutex
data map[string]*Storage
}
func NewSessionStorage() *SessionStorage {
return &SessionStorage{data: make(map[string]*Storage)}
}
func (s *SessionStorage) Set(taskName, handleName string, jobId, expireAt int64) {
s.mu.Lock()
defer s.mu.Unlock()
s.data[taskName] = &Storage{
HandleName: handleName,
ExpireAt: expireAt,
JobId: jobId,
}
}
func (s *SessionStorage) Get(taskName string) *Storage {
s.mu.Lock()
defer s.mu.Unlock()
storage, exists := s.data[taskName]
if !exists {
return nil
}
return storage
}
func (s *SessionStorage) Del(key string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.data, key)
}
func (s *SessionStorage) GetAll() map[string]*Storage {
s.mu.Lock()
defer s.mu.Unlock()
data := make(map[string]*Storage, len(s.data))
for k, v := range s.data {
data[k] = v
}
return data
}
func (s *SessionStorage) Len() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.data)
}
func (s *SessionStorage) Exists(key string) bool {
s.mu.Lock()
defer s.mu.Unlock()
_, exists := s.data[key]
if !exists {
return false
}
return true
}