- 添加通用查询选项、分类过滤选项及动态缓存键生成 - 新增分类创建与更新时间字段 当前实现尚未完成,留待后续继续设计: - 明确通用查询选项、模型过滤条件与缓存策略的边界 - 补齐数据库回源,修正分类返回类型及过滤条件生效问题 - 修正分页偏移计算,增加参数边界校验和稳定排序 - 完善缓存故障降级、空结果缓存及写后失效策略 - 修正缓存键参数编码歧义,补齐查询名和版本前缀 - 补充测试并验证编译与查询行为
83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
package query
|
|
|
|
import "time"
|
|
|
|
const (
|
|
defaultPage = 1
|
|
defaultPageSize = 10
|
|
defaultIncludeDeleted = false
|
|
defaultWithCacheWrite = false
|
|
defaultCacheTTL = 5 * time.Minute
|
|
defaultNotFoundCacheTTL = 1 * time.Minute
|
|
)
|
|
|
|
type Option struct {
|
|
Page int
|
|
PageSize int
|
|
IncludeDeleted bool
|
|
WithCacheWrite bool
|
|
CacheTTL time.Duration
|
|
NotFoundCacheTTL time.Duration
|
|
}
|
|
|
|
type OptionFunc func(*Option)
|
|
|
|
func NewOption(opts ...OptionFunc) *Option {
|
|
queryOpntion := &Option{
|
|
Page: defaultPage,
|
|
PageSize: defaultPageSize,
|
|
IncludeDeleted: defaultIncludeDeleted,
|
|
WithCacheWrite: defaultWithCacheWrite,
|
|
CacheTTL: defaultCacheTTL,
|
|
NotFoundCacheTTL: defaultNotFoundCacheTTL,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(queryOpntion)
|
|
}
|
|
return queryOpntion
|
|
}
|
|
|
|
func WithPage(page int) OptionFunc {
|
|
return func(o *Option) {
|
|
if page > 0 {
|
|
o.Page = page
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithPageSize(pageSize int) OptionFunc {
|
|
return func(o *Option) {
|
|
if pageSize > 0 {
|
|
o.PageSize = pageSize
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithIncludeDeleted(includeDeleted bool) OptionFunc {
|
|
return func(o *Option) {
|
|
o.IncludeDeleted = includeDeleted
|
|
}
|
|
}
|
|
|
|
func WithCacheWrite(withCacheWrite bool) OptionFunc {
|
|
return func(o *Option) {
|
|
o.WithCacheWrite = withCacheWrite
|
|
}
|
|
}
|
|
|
|
func WithCacheTTL(cacheTTL time.Duration) OptionFunc {
|
|
return func(o *Option) {
|
|
if cacheTTL >= 0 {
|
|
o.CacheTTL = cacheTTL
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithNotFoundCacheTTL(notFoundCacheTTL time.Duration) OptionFunc {
|
|
return func(o *Option) {
|
|
if notFoundCacheTTL >= 0 {
|
|
o.NotFoundCacheTTL = notFoundCacheTTL
|
|
}
|
|
}
|
|
}
|