补充db层数据清理锁安全
This commit is contained in:
parent
cc0d94ea6c
commit
ac1b3b0be2
@ -1,6 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,6 +25,7 @@ type (
|
|||||||
//过期数据
|
//过期数据
|
||||||
ModelDataExpired struct {
|
ModelDataExpired struct {
|
||||||
key string //主key
|
key string //主key
|
||||||
|
mu sync.RWMutex //安全锁
|
||||||
keys map[string]struct{} //数据集合
|
keys map[string]struct{} //数据集合
|
||||||
expired time.Time //过期时间
|
expired time.Time //过期时间
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,9 @@ func (this *DBConn) UpDateModelExpired(key string, childs map[string]struct{}, e
|
|||||||
exp.keys = make(map[string]struct{})
|
exp.keys = make(map[string]struct{})
|
||||||
}
|
}
|
||||||
for k, _ := range childs {
|
for k, _ := range childs {
|
||||||
|
exp.mu.Lock()
|
||||||
exp.keys[k] = struct{}{}
|
exp.keys[k] = struct{}{}
|
||||||
|
exp.mu.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exp.expired = time.Now().Add(expired)
|
exp.expired = time.Now().Add(expired)
|
||||||
@ -107,9 +109,11 @@ func (this *DBConn) scanning() {
|
|||||||
for _, v := range temp {
|
for _, v := range temp {
|
||||||
pipe.Del(ctx, v.key)
|
pipe.Del(ctx, v.key)
|
||||||
if v.keys != nil {
|
if v.keys != nil {
|
||||||
|
v.mu.RLock()
|
||||||
for k1, _ := range v.keys {
|
for k1, _ := range v.keys {
|
||||||
pipe.Del(ctx, k1)
|
pipe.Del(ctx, k1)
|
||||||
}
|
}
|
||||||
|
v.mu.RUnlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, err := pipe.Exec(ctx); err != nil {
|
if _, err := pipe.Exec(ctx); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user