package db import ( "context" "time" ) //更新数据模块过期 func (this *DB) UpDateModelExpired(key string, childs map[string]struct{}, expired time.Duration) { this.mu.RLock() exp, ok := this.data[key] this.mu.RUnlock() if ok { if childs != nil { if exp.keys == nil { exp.keys = make(map[string]struct{}) } for k, _ := range childs { exp.keys[k] = struct{}{} } } exp.expired = time.Now().Add(expired) } else { exp = &ModelDataExpired{ key: key, keys: childs, expired: time.Now().Add(expired), } this.mu.Lock() this.data[key] = exp this.mu.Unlock() } } //定时清理过期数据 func (this *DB) run() { timer := time.NewTicker(time.Minute * 1) defer timer.Stop() for { select { case <-timer.C: this.scanning() break } } } //扫描过期 func (this *DB) scanning() { now := time.Now() this.mu.Lock() temp := make([]*ModelDataExpired, 0, len(this.data)) for k, v := range this.data { if v.expired.Before(now) { //过期 temp = append(temp, v) delete(this.data, k) } } this.mu.Unlock() ctx := context.Background() pipe := this.local.Redis.Pipeline() for _, v := range temp { pipe.Del(ctx, v.key) if v.keys != nil { for k1, _ := range v.keys { pipe.Del(ctx, k1) } } } if _, err := pipe.Exec(ctx); err != nil { this.options.Log.Errorln(err) } }