enhance(cache):增加快速处理可空值作为Redis缓存键的处理。
This commit is contained in:
parent
cadb2db8c7
commit
e366888608
24
cache/abstract.go
vendored
24
cache/abstract.go
vendored
|
@ -137,3 +137,27 @@ func CacheKey(category string, ids ...string) string {
|
||||||
}
|
}
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ToString[T any] interface {
|
||||||
|
ToString() string
|
||||||
|
*T
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用于生成一个内容可以为空的Redis缓存键,这个键的类型必须实现了`ToString`接口。
|
||||||
|
func NullableConditionKey[P any, T ToString[P]](value T, defaultStr ...string) string {
|
||||||
|
defaultStr = append(defaultStr, "UNDEF")
|
||||||
|
if value == nil {
|
||||||
|
return defaultStr[0]
|
||||||
|
} else {
|
||||||
|
return value.ToString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用于生成一个内容可以为空的字符串指针类型的Redis缓存键。
|
||||||
|
func NullableStringKey(value *string, defaultStr ...string) string {
|
||||||
|
defaultStr = append(defaultStr, "UNDEF")
|
||||||
|
if value == nil {
|
||||||
|
return defaultStr[0]
|
||||||
|
}
|
||||||
|
return *value
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user