12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package utils
- import (
- "gd_service/errors"
- "time"
- "gd_service/apis"
- "gd_service/common.in/cache"
- "gd_service/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 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
- }
|