voucher/internal/biz/vo/cache.go

102 lines
2.2 KiB
Go

package vo
import (
"time"
"voucher/internal/pkg/helper"
)
type CacheKey string
const (
CmbOrderLockKey CacheKey = "cmb_order"
CmbQueryLockKey CacheKey = "cmb_query"
CmbProductQueryLockKey CacheKey = "cmb_product_query"
CmbBatchNoticeCacheKey CacheKey = "cmb_batch_notice"
CmbBatchNoticeLockKey CacheKey = "cmb_batch_notice_lock"
NotifyRetryConsume CacheKey = "notify_retry_consume"
OrderConsumeFailAlarmKey CacheKey = "order_consume_fail_alarm"
OrderConsumeFailAlarmLockKey CacheKey = "order_consume_fail_alarm_lock"
WechatNotifyRegisterTagCacheKey CacheKey = "register"
WechatNotifyRegisterTagCacheLockKey CacheKey = "register_tag_lock"
WechatNotifyConsumeLockKey CacheKey = "wechat_notify_consume"
)
const (
ProductQueryKey CacheKey = "product_query"
ProductQueryLockKey CacheKey = "product_query_lock"
)
var CacheKeyMap = map[CacheKey]time.Duration{
CmbOrderLockKey: 30 * time.Second,
CmbQueryLockKey: 30 * time.Second,
CmbProductQueryLockKey: 30 * time.Second,
CmbBatchNoticeCacheKey: 21600 * time.Second, // 6小时
CmbBatchNoticeLockKey: 300 * time.Second,
OrderConsumeFailAlarmKey: 5 * time.Hour, // 5小时
OrderConsumeFailAlarmLockKey: 60 * time.Second,
NotifyRetryConsume: 60 * time.Second,
WechatNotifyRegisterTagCacheKey: 30 * 86400 * time.Second, // 30天
WechatNotifyRegisterTagCacheLockKey: 60 * time.Second,
WechatNotifyConsumeLockKey: 30 * time.Second,
ProductQueryKey: 15 * 86400 * time.Second, // 15天
ProductQueryLockKey: 30 * time.Second,
}
type Cache struct {
Key string
TTL time.Duration
}
func (s CacheKey) GetValue() string {
return string(s)
}
func (s CacheKey) BuildCache(strArr []string) *Cache {
k := s.GetValue()
if len(strArr) > 0 {
k = helper.BuildStr(k, strArr)
}
c := &Cache{
Key: k,
}
ttl, ok := CacheKeyMap[s]
if !ok {
c.TTL = 30 // 默认30秒
}
c.TTL = ttl
return c
}
func (s CacheKey) BuildCacheUint64(ids []uint64) *Cache {
k := s.GetValue()
if len(ids) > 0 {
k = helper.BuildStr(k, ids)
}
c := &Cache{
Key: k,
}
ttl, ok := CacheKeyMap[s]
if !ok {
c.TTL = 30 // 默认30秒
}
c.TTL = ttl
return c
}