package utils import ( "gd_vehicle/errors" "time" "gd_vehicle/apis" "gd_vehicle/common.in/cache" "gd_vehicle/consts" "github.com/json-iterator/go" ) func IsReuseTime(reuseTime int64, timestamp int64) bool { if timestamp == 0 { return false } reuseSencod := reuseTime * consts.DAYSECOND t := time.Unix(timestamp, 0) now := time.Now() // 60s短时复用 if reuseTime == 0 { if now.Unix()-timestamp > consts.MINUTESECOND { return false } return true } start := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix() end := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix() if end-start >= reuseSencod { return false } return true } func GetDataByRedis(redisKey string, reuseTime int64, fn func(data string) (string, error)) (string, error) { res, err := cache.Redis.Get(redisKey) if err == nil && res != "" { rawData := apis.RawData{} _ = jsoniter.UnmarshalFromString(res, &rawData) if IsReuseTime(reuseTime, rawData.Timestamp) { return fn(rawData.Data) } } return "",errors.ServiceError } func GetRawDataByRedis(redisKey string, reuseTime int64) (string, error) { res, err := cache.Redis.Get(redisKey) if err == nil && res != "" { rawData := apis.RawData{} _ = jsoniter.UnmarshalFromString(res, &rawData) if IsReuseTime(reuseTime, rawData.Timestamp) { if rawData.Data == ""{ return "",errors.NoRecord } return rawData.Data,nil } } return "",errors.ServiceError } func StoreDataToRedis(redisKey, data string) { store := apis.RawData{ Data: data, Timestamp: time.Now().Unix(), } str, _ := jsoniter.MarshalToString(store) _, _ = cache.Redis.SetEx(redisKey, consts.RedisDataExipred, str) return }