voucher/internal/biz/vo/cache.go

78 lines
1.6 KiB
Go

package vo
import (
"fmt"
"time"
)
type CacheKey string
const (
CmbOrderLockKey CacheKey = "cmb_order"
CmbQueryLockKey CacheKey = "cmb_query"
CmbProductQueryLockKey CacheKey = "cmb_product_query"
OrderConsume CacheKey = "order_consume"
NotifyConsume CacheKey = "notify_consume"
WechatNotifyRegisterTagCacheKey CacheKey = "wechat_notify_register_tag"
WechatNotifyRegisterTagCacheLockKey CacheKey = "wechat_notify_register_tag_lock"
WechatNotifyConsumeLockKey CacheKey = "wechat_notify_consume"
)
var CacheKeyMap = map[CacheKey]time.Duration{
CmbOrderLockKey: 30 * time.Second,
CmbQueryLockKey: 30 * time.Second,
CmbProductQueryLockKey: 30 * time.Second,
OrderConsume: 30 * time.Second,
NotifyConsume: 30 * time.Second,
WechatNotifyRegisterTagCacheKey: 86400 * time.Second,
WechatNotifyRegisterTagCacheLockKey: 30 * time.Second,
WechatNotifyConsumeLockKey: 30 * time.Second,
}
type Cache struct {
Key string
TTL time.Duration
}
func (s CacheKey) BuildCache(ids []string) *Cache {
k := fmt.Sprintf("%s", s)
for _, id := range ids {
k = fmt.Sprintf("%s_%s", k, id)
}
c := &Cache{
Key: k,
}
ttl, ok := CacheKeyMap[s]
if !ok {
c.TTL = 30 // 默认30秒
}
c.TTL = ttl
return c
}
func (s CacheKey) BuildRegisterCache(tag, stockCreatorMchID, stockID string) *Cache {
k := fmt.Sprintf("%s_%s_%s_%s", s, tag, stockCreatorMchID, stockID)
c := &Cache{
Key: k,
}
ttl, ok := CacheKeyMap[s]
if !ok {
c.TTL = 30 // 默认30秒
}
c.TTL = ttl
return c
}