template-monorepo/internal/model/notification/redis.go

39 lines
1.1 KiB
Go

package notification
import "strings"
// RedisKey is the notification module key prefix.
type RedisKey string
const (
IdempotencyRedisKey RedisKey = "notif:idem"
QuotaRedisKey RedisKey = "notif:quota"
RetryZSetRedisKey RedisKey = "notif:retry:zset"
)
func (key RedisKey) With(parts ...string) RedisKey {
if len(parts) == 0 {
return key
}
return RedisKey(string(key) + ":" + strings.Join(parts, ":"))
}
func (key RedisKey) String() string {
return string(key)
}
// GetIdempotencyRedisKey caches a prior send result: notif:idem:{tenant}:{kind}:{idempotencyKey}.
func GetIdempotencyRedisKey(tenantID, kind, idempotencyKey string) string {
return IdempotencyRedisKey.With(tenantID, kind, idempotencyKey).String()
}
// GetQuotaRedisKey is the daily counter: notif:quota:{tenant}:{channel}:{yyyyMMdd}.
func GetQuotaRedisKey(tenantID, channel, day string) string {
return QuotaRedisKey.With(tenantID, channel, day).String()
}
// GetRetryZSetRedisKey is the async retry schedule zset.
func GetRetryZSetRedisKey() string {
return RetryZSetRedisKey.String()
}