scan.go 2.67 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4 5
package redis_r

import (
	"context"
	"fmt"
hujiebin's avatar
hujiebin committed
6 7 8 9
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/config"
chenweijian's avatar
chenweijian committed
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
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
	"strconv"
	"strings"
	"time"
)

var stop = make(chan struct{})
var match = "match_relation_*"

func Scan(c *gin.Context) (*mycontext.MyContext, error) {
	go func() {
		return
		myCtx := mycontext.CreateMyContext(c.Keys)
		var model = domain.CreateModelContext(myCtx)
		cursor := uint64(0)
		first := true
		var err error
		var keys []string
		for first || cursor != 0 {
			first = false
			select {
			case <-stop:
				return
			default:
				keys, cursor, err = model.Redis.Scan(model, cursor, match, 1000).Result()
				if err != nil {
					model.Log.Errorf("SCAN fail:%v", err)
					return
				}
				var delKeys []string
				for _, key := range keys {
					arr := strings.Split(key, "_")
					if len(arr) == 4 {
						cycle, _ := strconv.ParseInt(arr[2], 10, 64)
						if cycle > 0 && cycle < 208933198 { // 2022-12-20号
							delKeys = append(delKeys, key)
						}
					}
				}
				if len(delKeys) > 0 {
					cnt, err := model.Redis.Del(model, keys...).Result()
					model.Log.Infof("del redis keys:%v,cnt:%v,err:%v", delKeys, cnt, err)
				}
				time.Sleep(time.Second)
			}
		}
	}()
	return mycontext.CreateMyContext(nil), nil
}

func ScanStop(c *gin.Context) (*mycontext.MyContext, error) {
	return nil, nil
	myCtx := mycontext.CreateMyContext(c.Keys)
	stop <- struct{}{}
	return myCtx, nil
}

func Hscan(c *gin.Context) (*mycontext.MyContext, error) {
	RedisClient := redis.NewClient(&redis.Options{
		Addr:         config.GetConfigRedis().REDIS_HOST,
		Password:     config.GetConfigRedis().REDIS_PASSWORD, // no password set
		DB:           1,                                      // use default DB
		PoolSize:     20,
		MinIdleConns: 20,
	})
	cursor := uint64(0)
	first := true
	var err error
	var keys []string
	for first || cursor != 0 {
		first = false
		select {
		case <-stop:
			return nil, nil
		default:
			keys, cursor, err = RedisClient.HScan(context.Background(), "user", cursor, "*", 1000).Result()
			if err != nil {
				return nil, err
			}
			l := len(keys)
			if l%2 != 0 {
				mylogrus.MyLog.Errorf("Hscan keys err:%v", keys)
				continue
			}
			for i := 0; i < len(keys); i += 2 {
				if !strings.Contains(keys[i+1], "172.26.95.48:50050") && !strings.Contains(keys[i+1], "172.26.95.24:50050") {
					res, err := RedisClient.HDel(context.Background(), "user", keys[i]).Result()
					msg := fmt.Sprintf("hdel user %v,value:%v,res:%v,err:%v", keys[i], keys[i+1], res, err)
					mylogrus.MyLog.Infof("%v", msg)
				}
			}
		}
	}
	return nil, nil
}