rankclean.go 1.76 KB
package main

import (
	"client/sdkdb"
	"client/sdkredis"
	"common"
	"flag"
	"fmt"
	"log"
	"os"
	"strings"
)

func main() {
	configPath := flag.String("config", "./sdkRoute.conf", "config path")
	flag.Parse()
	fmt.Printf("config[%v]", *configPath)
	config := common.NewConfig(*configPath)

	// db初始化
	sdkdb.DBInit()
	// 注册数据库
	dberr := sdkdb.DBRegis(config.DBMysql)
	if dberr != nil {
		log.Printf("DBRegis err[%v]\n", dberr)
		return
	}

	// 设置数据库日志
	sdkdb.SetLogDebug(true, os.Stdout)

	// redis 初始化
	sdkredis.RedisInit(config)

	// 连接redis
	redisConn := sdkredis.RedisClient.GetConn()
	defer sdkredis.RedisClose(redisConn)

	for {
		msg, _ := sdkredis.RedisLPop(redisConn, "IsRankClean")
		if msg == "clean" {
			keys, err := sdkredis.RedisGetKeys(redisConn, "ice_gametop*toptype:[^0]*mykey")
			if err != nil {
				sdkredis.RedisClose(redisConn)
				log.Printf("RedisGetKeys keys[%s] err[%v]\n", keys, err)
				continue
			}

			log.Printf("clean start...")

			// redis 更新
			for _, key := range keys {
				index0 := strings.Index(key, ":gtid:")
				index1 := strings.Index(key, ":gid:")
				index2 := strings.Index(key, ":toptype:")
				index3 := strings.Index(key, ":mykey")
				gtid := common.Substr2(key, index0+6, index1)
				gid := common.Substr2(key, index1+5, index2)
				toptype := common.Substr2(key, index2+9, index3)

				//sdkredis.RedisSetKV(redisConn, key, "0")
				//sdkredis.RedisZAddSortSet(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, 0, key)
				log.Printf("del key[%v]", key)
				sdkredis.RedisDelKey(redisConn, key)
				sdkredis.RedisZSortSetRem(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, key)
			}

			// 数据库更新
			sdkdb.IceGameTopCleanScore()

			log.Printf("clean end.")
		}
	}
}