-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_ttl.go
175 lines (168 loc) · 3.16 KB
/
map_ttl.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package map_ttl
import (
"sync"
"time"
)
const (
Del = 1
Reset = 2
)
type data struct {
value interface{}
CloseChan chan comData
TimeoutTime time.Time
}
type comData struct {
Flag int
NewTtl time.Duration
timeoutDeleteFlag bool
}
type MapTtl struct {
sync.RWMutex
data map[interface{}]data
callbackChan *chan interface{}
}
func (slf *MapTtl) tll(key interface{}, ttl time.Duration, _chan chan comData, DeleteFlag bool) {
timeoutDeleteFlag := DeleteFlag
for {
var timeoutChan <-chan time.Time
if ttl > 0 {
timeoutChan = time.After(ttl)
}
select {
case <-timeoutChan:
slf.Lock()
if slf.callbackChan != nil {
v, ok := slf.data[key]
if ok {
*slf.callbackChan <- v.value
}
}
if timeoutDeleteFlag == true {
delete(slf.data, key)
slf.Unlock()
break
}
slf.Unlock()
case data := <-_chan:
if data.Flag == Del {
delete(slf.data, key)
} else if data.Flag == Reset {
ttl = data.NewTtl
timeoutDeleteFlag = data.timeoutDeleteFlag
}
}
}
}
func (slf *MapTtl) SetCallback(callbackChan *chan interface{}) {
slf.Lock()
defer slf.Unlock()
slf.callbackChan = callbackChan
}
func (slf *MapTtl) Init() {
slf.data = make(map[interface{}]data)
}
func (slf *MapTtl) SetData(key, value interface{}) bool {
slf.Lock()
defer slf.Unlock()
if slf.data != nil {
if v, ok := slf.data[key]; ok {
v.value = value
slf.data[key] = v
return true
}
}
return false
}
func (slf *MapTtl) UnsafeSetData(key, value interface{}) bool {
if slf.data != nil {
if v, ok := slf.data[key]; ok {
v.value = value
slf.data[key] = v
return true
}
}
return false
}
func (slf *MapTtl) Set(key, value interface{}, ttl time.Duration, TimeOutDelete bool) {
slf.Lock()
defer slf.Unlock()
if slf.data != nil {
if v, ok := slf.data[key]; ok {
v.CloseChan <- comData{
Flag: Reset,
NewTtl: ttl,
}
}
CloseChan := make(chan comData)
slf.data[key] = data{
TimeoutTime: time.Now().Add(ttl),
CloseChan: CloseChan,
value: value,
}
go slf.tll(key, ttl, CloseChan, TimeOutDelete)
}
}
func (slf *MapTtl) Get(key interface{}) interface{} {
slf.Lock()
defer slf.Unlock()
if slf.data != nil {
if v, ok := slf.data[key]; ok {
return v.value
}
}
return nil
}
func (slf *MapTtl) Del(key interface{}) {
slf.Lock()
defer slf.Unlock()
if slf.data != nil {
if v, ok := slf.data[key]; ok {
v.CloseChan <- comData{
Flag: Del,
}
}
}
}
func (slf *MapTtl) GetTtl(key interface{}) time.Duration {
slf.Lock()
defer slf.Unlock()
if slf.data != nil {
if v, ok := slf.data[key]; ok {
return v.TimeoutTime.Sub(time.Now())
}
}
return -1
}
func (slf *MapTtl) Clear() {
slf.Lock()
defer slf.Unlock()
for _, data := range slf.data {
data.CloseChan <- comData{
Flag: Del,
}
}
for {
if len(slf.data) == 0 {
break
}
}
}
func (slf *MapTtl) Len() int {
slf.Lock()
defer slf.Unlock()
if slf.data == nil {
return 0
} else {
return len(slf.data)
}
}
func (slf *MapTtl) Range(f func(key interface{}, value interface{})) {
slf.Lock()
defer slf.Unlock()
if slf.data != nil {
for k, v := range slf.data {
f(k, v.value)
}
}
}