cache_medal.go 1.3 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4 5
package common

import (
	"context"
	"encoding/json"
hujiebin's avatar
hujiebin committed
6 7 8
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/resource/redisCli"
chenweijian's avatar
chenweijian committed
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
	"hilo-user/_const/redis_key"
	"hilo-user/myerr"
	"time"
)

func GetUserMedalMergeCache(userId mysql.ID) ([]uint32, error) {
	bData, err := GetCache(redis_key.GetUserMedalMerge(userId))
	if err != nil {
		return nil, myerr.WrapErr(err)
	}

	res := make([]uint32, 0)
	err = json.Unmarshal(bData, &res)
	if err != nil {
		return nil, myerr.WrapErr(err)
	}
	return res, nil
}

func GetCache(key string) ([]byte, error) {
	data, err := redisCli.GetRedis().Get(context.Background(), key).Bytes()
	if err != nil {
		return nil, err
	}
	return data, nil
}

func SetCache(key string, data interface{}, expire time.Duration) error {
	jData, err := json.Marshal(data)
	if err != nil {
		return err
	}
	err = redisCli.GetRedis().Set(context.Background(), key, jData, expire).Err()
	if err != nil {
		mylogrus.MyLog.Errorf("SetCache key:%s, data:%v, err:%s", key, data, err)
		return err
	}
	return nil
}

func SetUserMedalMergeCache(userId mysql.ID, data []uint32) error {
	err := SetCache(redis_key.GetUserMedalMerge(userId), data, time.Hour*2)
	if err != nil {
		mylogrus.MyLog.Errorf("SetUserMedalMerge err:%s", err)
		return myerr.WrapErr(err)
	}
	return nil
}