http_router

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchDeleteRequest

type BatchDeleteRequest struct {
	IDs     []interface{} `json:"ids,omitempty"`     // ID列表
	Soft    bool          `json:"soft"`              // 是否软删除
	Filters []interface{} `json:"filters,omitempty"` // 过滤条件(预留)
}

BatchDeleteRequest 批量删除请求

type BatchIncrementRequest

type BatchIncrementRequest struct {
	IDs     []interface{} `json:"ids,omitempty"`     // ID列表
	Column  string        `json:"column"`            // 字段名
	Value   interface{}   `json:"value"`             // 增量值
	IsDecr  bool          `json:"is_decr,omitempty"` // 是否为减量操作
	Filters []interface{} `json:"filters,omitempty"` // 过滤条件(预留)
}

BatchIncrementRequest 批量增量请求

type BatchUpdateRequest

type BatchUpdateRequest struct {
	Updates map[string]interface{} `json:"updates"`           // 更新字段
	Filters []interface{}          `json:"filters,omitempty"` // 过滤条件(预留)
}

BatchUpdateRequest 批量更新请求

type BatchUpsertRequest

type BatchUpsertRequest[T any] struct {
	Data            []T      `json:"data"`
	ConflictColumns []string `json:"conflict_columns"` // 冲突字段
	UpdateColumns   []string `json:"update_columns"`   // 更新字段(为空则全部更新)
	BatchSize       int      `json:"batch_size"`       // 批次大小,默认100
}

BatchUpsertRequest 批量Upsert请求

type CountRequest

type CountRequest struct {
	Filters []filter_translator.FilterParam `json:"filters"`
}

type CountResponse

type CountResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Count   int64  `json:"count"`
}

type DeleteRequest

type DeleteRequest struct {
	ID   interface{} `json:"id"`   // 可选,如果提供则按ID删除
	Soft bool        `json:"soft"` // 是否软删除
}

DeleteRequest 删除请求

type HTTPRouterManager

type HTTPRouterManager[T any] struct {
	// 1. 使用指针嵌入 (*),避免拷贝
	// 2. 必须显式传递泛型参数 [T]
	*serviceManager.ServiceManager[T]
}

HTTPRouterManager 封装了 ServiceManager 并提供 HTTP 路由注册功能

func NewHTTPRouterManager

func NewHTTPRouterManager[T any](svc *serviceManager.ServiceManager[T]) *HTTPRouterManager[T]

NewHTTPRouterManager 构造函数 (推荐方式:依赖注入) 接收一个已经初始化好的 ServiceManager

func NewHTTPRouterManagerFromModel

func NewHTTPRouterManagerFromModel[T any](model T) *HTTPRouterManager[T]

NewHTTPRouterManagerFromModel 构造函数 (兼容你原来的方式) 接收模型实例,在内部创建新的 ServiceManager 注意:如果在多个地方使用,可能会导致创建多个 DB 连接池,请谨慎使用

type IncrementRequest

type IncrementRequest struct {
	ID     interface{} `json:"id"`                // 可选,如果提供则按ID操作
	Column string      `json:"column"`            // 字段名
	Value  interface{} `json:"value"`             // 增量值
	IsDecr bool        `json:"is_decr,omitempty"` // 是否为减量操作
}

IncrementRequest 增量请求

type InvalidateRequest

type InvalidateRequest struct {
	Keys    []string `json:"keys"`    // 精确键列表
	Pattern string   `json:"pattern"` // 或使用模式
}

type InvalidateResponse

type InvalidateResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Count   int    `json:"count"` // 删除的键数量
}

type LookupCountRequest

type LookupCountRequest struct {
	KeyPattern      string                          `json:"key_pattern"`
	Filters         []filter_translator.FilterParam `json:"filters"`
	UseCustomFilter bool                            `json:"use_custom_filter"`
}

type LookupCountResponse

type LookupCountResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Count   int    `json:"count"`
}

type LookupRequest

type LookupRequest struct {
	KeyPattern      string                          `json:"key_pattern"`       // 可选,覆盖默认 key 模式
	Filters         []filter_translator.FilterParam `json:"filters"`           // 过滤条件
	UseCustomFilter bool                            `json:"use_custom_filter"` // 是否使用自定义过滤器
	FallbackToDB    bool                            `json:"fallback_db"`       // 是否回源数据库
}

type LookupResponse

type LookupResponse[T any] struct {
	Code    int           `json:"code"`
	Message string        `json:"message"`
	Data    map[string]*T `json:"data"`
	Keys    []string      `json:"keys"`
	Count   int           `json:"count"`
}

type LookupRouterGroup

type LookupRouterGroup[T any] struct {
	RouterGroup        *gin.RouterGroup
	Service            *service.ServiceManager[T]
	TranslatorRegistry *filter_translator.RedisTranslatorRegistry
	// contains filtered or unexported fields
}

func NewLookupRouterGroup

func NewLookupRouterGroup[T any](
	rg *gin.RouterGroup,
	service *service.ServiceManager[T],
) *LookupRouterGroup[T]

func (*LookupRouterGroup[T]) HandleCount

func (lrg *LookupRouterGroup[T]) HandleCount(c *gin.Context)

func (*LookupRouterGroup[T]) HandleGetByKey

func (lrg *LookupRouterGroup[T]) HandleGetByKey(c *gin.Context)

HandleGetByKey 使用 Cache Aside 模式处理单个键查询

func (*LookupRouterGroup[T]) HandleInvalidate

func (lrg *LookupRouterGroup[T]) HandleInvalidate(c *gin.Context)

func (*LookupRouterGroup[T]) HandleLookup

func (lrg *LookupRouterGroup[T]) HandleLookup(c *gin.Context)

func (*LookupRouterGroup[T]) RegisterRoutes

func (lrg *LookupRouterGroup[T]) RegisterRoutes(basePath string)

func (*LookupRouterGroup[T]) SetCacheAsideConfig

func (lrg *LookupRouterGroup[T]) SetCacheAsideConfig(ttl time.Duration, refreshOnHit bool) *LookupRouterGroup[T]

SetCacheAsideConfig 设置 Cache Aside 模式配置

func (*LookupRouterGroup[T]) SetCustomFilter

func (lrg *LookupRouterGroup[T]) SetCustomFilter(
	filterFunc func(context.Context, *redis.Client, []string) ([]string, error),
) *LookupRouterGroup[T]

SetCustomFilter 设置自定义过滤函数(如活跃用户过滤)

func (*LookupRouterGroup[T]) SetDefaults

func (lrg *LookupRouterGroup[T]) SetDefaults(keyPattern string, cacheExpire time.Duration) *LookupRouterGroup[T]

SetDefaults 设置默认的 key 模式和缓存过期时间

func (*LookupRouterGroup[T]) SetKeyBuilder

func (lrg *LookupRouterGroup[T]) SetKeyBuilder(builder func(uint) string) *LookupRouterGroup[T]

SetKeyBuilder 设置从ID构建key的函数

type PaginatedQueryMethod

type PaginatedQueryMethod[T any] struct {
	Name       string
	PageSize   int
	Service    *service.ServiceManager[T]
	FilterFunc func(*gorm.DB) *gorm.DB
	OrderBy    string
	Order      string
}

func (*PaginatedQueryMethod[T]) Execute

func (m *PaginatedQueryMethod[T]) Execute(ctx context.Context, page int, filters []filter_translator.GormFilter) (*service.QueryResult[T], error)

type QueryMethodRegistry

type QueryMethodRegistry[T any] struct {
	// contains filtered or unexported fields
}

func NewQueryMethodRegistry

func NewQueryMethodRegistry[T any]() *QueryMethodRegistry[T]

func (*QueryMethodRegistry[T]) Get

func (r *QueryMethodRegistry[T]) Get(name string) (*PaginatedQueryMethod[T], bool)

func (*QueryMethodRegistry[T]) Register

func (r *QueryMethodRegistry[T]) Register(method *PaginatedQueryMethod[T])

type QueryRequest

type QueryRequest struct {
	Method  string                          `json:"method"`
	Page    int                             `json:"page"`
	Filters []filter_translator.FilterParam `json:"filters"`
}

type QueryResponse

type QueryResponse[T any] struct {
	Code       int    `json:"code"`
	Message    string `json:"message"`
	Data       []T    `json:"data"`
	Total      int64  `json:"total"`
	Page       int    `json:"page"`
	PageSize   int    `json:"page_size"`
	TotalPages int    `json:"total_pages"`
}

type QueryRouterGroup

type QueryRouterGroup[T any] struct {
	RouterGroup    *gin.RouterGroup
	Service        *service.ServiceManager[T]
	MethodRegistry *QueryMethodRegistry[T]
	// 【变更2】类型改为 GormTranslatorRegistry (根据示例推断)
	TranslatorRegistry *filter_translator.GormTranslatorRegistry
}

func NewQueryRouterGroup

func NewQueryRouterGroup[T any](
	rg *gin.RouterGroup,
	service *service.ServiceManager[T],
) *QueryRouterGroup[T]

func (*QueryRouterGroup[T]) HandleCount

func (qrg *QueryRouterGroup[T]) HandleCount(c *gin.Context)

func (*QueryRouterGroup[T]) HandleGetByID

func (qrg *QueryRouterGroup[T]) HandleGetByID(c *gin.Context)

func (*QueryRouterGroup[T]) HandleQuery

func (qrg *QueryRouterGroup[T]) HandleQuery(c *gin.Context)

func (*QueryRouterGroup[T]) RegisterActiveListMethod

func (qrg *QueryRouterGroup[T]) RegisterActiveListMethod(pageSize int)

func (*QueryRouterGroup[T]) RegisterCommonMethods

func (qrg *QueryRouterGroup[T]) RegisterCommonMethods(defaultPageSize int)

func (*QueryRouterGroup[T]) RegisterListMethod

func (qrg *QueryRouterGroup[T]) RegisterListMethod(pageSize int)

func (*QueryRouterGroup[T]) RegisterMethod

func (qrg *QueryRouterGroup[T]) RegisterMethod(name string, pageSize int, filterFunc func(*gorm.DB) *gorm.DB, orderBy string, order string)

func (*QueryRouterGroup[T]) RegisterRoutes

func (qrg *QueryRouterGroup[T]) RegisterRoutes(basePath string)

func (*QueryRouterGroup[T]) RegisterSearchMethod

func (qrg *QueryRouterGroup[T]) RegisterSearchMethod(pageSize int, searchFields []string)

type RefreshCacheRequest

type RefreshCacheRequest struct {
	Key        string      `json:"key"`
	ID         interface{} `json:"id"`
	Expiration int64       `json:"expiration_seconds,omitempty"`
}

type SetQueryRequest

type SetQueryRequest[T any] struct {
	Data             []T  `json:"data"`
	BatchSize        int  `json:"batch_size"`         // 默认 100
	OnConflictUpdate bool `json:"on_conflict_update"` // 默认 true
	InvalidateCache  bool `json:"invalidate_cache"`   // 默认 true
}

SetQueryRequest 批量设置请求

type SetSingleRequest

type SetSingleRequest[T any] struct {
	Data             *T   `json:"data"`
	OnConflictUpdate bool `json:"on_conflict_update"` // 默认 true
	InvalidateCache  bool `json:"invalidate_cache"`   // 默认 true
}

SetSingleRequest 单个设置请求

type UpdateRequest

type UpdateRequest struct {
	ID      interface{}            `json:"id"`      // 可选,如果提供则按ID更新
	Updates map[string]interface{} `json:"updates"` // 更新字段
}

UpdateRequest 更新请求

type UpsertRequest

type UpsertRequest[T any] struct {
	Data            *T       `json:"data"`
	ConflictColumns []string `json:"conflict_columns"` // 冲突字段
	UpdateColumns   []string `json:"update_columns"`   // 更新字段(为空则全部更新)
}

UpsertRequest Upsert请求

type WarmupCacheRequest

type WarmupCacheRequest struct {
	KeyTemplate string `json:"key_template"`
	Limit       int    `json:"limit,omitempty"`
	OrderBy     string `json:"order_by,omitempty"`
	Expiration  int64  `json:"expiration_seconds,omitempty"`
}

type WriteResponse

type WriteResponse struct {
	Code         int    `json:"code"`
	Message      string `json:"message"`
	RowsAffected int64  `json:"rows_affected,omitempty"` // 影响的行数
}

WriteResponse 写操作响应

type WriteRouterGroup

type WriteRouterGroup[T any] struct {
	RouterGroup *gin.RouterGroup
	Service     *service.ServiceManager[T]
}

func NewWriteRouterGroup

func NewWriteRouterGroup[T any](
	rg *gin.RouterGroup,
	service *service.ServiceManager[T],
) *WriteRouterGroup[T]

func (*WriteRouterGroup[T]) HandleBatchDelete

func (wrg *WriteRouterGroup[T]) HandleBatchDelete(c *gin.Context)

HandleBatchDelete 处理批量删除

func (*WriteRouterGroup[T]) HandleBatchIncrement

func (wrg *WriteRouterGroup[T]) HandleBatchIncrement(c *gin.Context)

HandleBatchIncrement 处理批量增量/减量

func (*WriteRouterGroup[T]) HandleBatchInsert

func (wrg *WriteRouterGroup[T]) HandleBatchInsert(c *gin.Context)

HandleBatchInsert 处理批量插入

func (*WriteRouterGroup[T]) HandleBatchUpdate

func (wrg *WriteRouterGroup[T]) HandleBatchUpdate(c *gin.Context)

HandleBatchUpdate 处理批量更新

func (*WriteRouterGroup[T]) HandleBatchUpsert

func (wrg *WriteRouterGroup[T]) HandleBatchUpsert(c *gin.Context)

HandleBatchUpsert 处理批量Upsert

func (*WriteRouterGroup[T]) HandleDelete

func (wrg *WriteRouterGroup[T]) HandleDelete(c *gin.Context)

HandleDelete 处理删除

func (*WriteRouterGroup[T]) HandleIncrement

func (wrg *WriteRouterGroup[T]) HandleIncrement(c *gin.Context)

HandleIncrement 处理增量/减量

func (*WriteRouterGroup[T]) HandleInsert

func (wrg *WriteRouterGroup[T]) HandleInsert(c *gin.Context)

HandleInsert 处理插入(不冲突更新)

func (*WriteRouterGroup[T]) HandleSetQuery

func (wrg *WriteRouterGroup[T]) HandleSetQuery(c *gin.Context)

HandleSetQuery 处理批量设置

func (*WriteRouterGroup[T]) HandleSetSingle

func (wrg *WriteRouterGroup[T]) HandleSetSingle(c *gin.Context)

HandleSetSingle 处理单个设置(新增或更新)

func (*WriteRouterGroup[T]) HandleUpdate

func (wrg *WriteRouterGroup[T]) HandleUpdate(c *gin.Context)

HandleUpdate 处理更新

func (*WriteRouterGroup[T]) HandleUpsert

func (wrg *WriteRouterGroup[T]) HandleUpsert(c *gin.Context)

HandleUpsert 处理Upsert

func (*WriteRouterGroup[T]) RegisterRoutes

func (wrg *WriteRouterGroup[T]) RegisterRoutes(basePath string)

type WritedownQueryRequest

type WritedownQueryRequest[T any] struct {
	Data        []T           `json:"data,omitempty"`
	IDs         []interface{} `json:"ids,omitempty"`
	LoadAll     bool          `json:"load_all,omitempty"`
	KeyTemplate string        `json:"key_template"`
	Expiration  int64         `json:"expiration_seconds,omitempty"`
	BatchSize   int           `json:"batch_size,omitempty"`
	Overwrite   bool          `json:"overwrite"`
	UsePipeline bool          `json:"use_pipeline,omitempty"`
	Incremental bool          `json:"incremental,omitempty"`
}

type WritedownResponse

type WritedownResponse[T any] struct {
	Code         int    `json:"code"`
	Message      string `json:"message"`
	ItemsWritten int    `json:"items_written,omitempty"`
	Data         *T     `json:"data,omitempty"`
}

type WritedownRouterConfig

type WritedownRouterConfig[T any] struct {
	KeyBuilder cache_key_builder.KeyBuilder[T]
}

type WritedownRouterGroup

type WritedownRouterGroup[T any] struct {
	RouterGroup *gin.RouterGroup
	Service     *service.ServiceManager[T]
	KeyBuilder  cache_key_builder.KeyBuilder[T]
}

func NewWritedownRouterGroup

func NewWritedownRouterGroup[T any](rg *gin.RouterGroup, svc *service.ServiceManager[T], cfg ...*WritedownRouterConfig[T]) *WritedownRouterGroup[T]

func (*WritedownRouterGroup[T]) HandleRefreshCache

func (wdg *WritedownRouterGroup[T]) HandleRefreshCache(c *gin.Context)

func (*WritedownRouterGroup[T]) HandleWarmupCache

func (wdg *WritedownRouterGroup[T]) HandleWarmupCache(c *gin.Context)

func (*WritedownRouterGroup[T]) HandleWritedownQuery

func (wdg *WritedownRouterGroup[T]) HandleWritedownQuery(c *gin.Context)

==================== 批量写入 ====================

func (*WritedownRouterGroup[T]) HandleWritedownSingle

func (wdg *WritedownRouterGroup[T]) HandleWritedownSingle(c *gin.Context)

func (*WritedownRouterGroup[T]) HandleWritedownWithLock

func (wdg *WritedownRouterGroup[T]) HandleWritedownWithLock(c *gin.Context)

func (*WritedownRouterGroup[T]) HandleWritedownWithVersion

func (wdg *WritedownRouterGroup[T]) HandleWritedownWithVersion(c *gin.Context)

func (*WritedownRouterGroup[T]) RegisterRoutes

func (wdg *WritedownRouterGroup[T]) RegisterRoutes(basePath string)

func (*WritedownRouterGroup[T]) SetKeyBuilder

func (wdg *WritedownRouterGroup[T]) SetKeyBuilder(builder cache_key_builder.KeyBuilder[T])

type WritedownSingleRequest

type WritedownSingleRequest[T any] struct {
	Key               string      `json:"key"`
	Data              *T          `json:"data,omitempty"`
	ID                interface{} `json:"id,omitempty"`
	ExpirationSeconds int64       `json:"expiration_seconds,omitempty"`
	Overwrite         bool        `json:"overwrite"`
	NX                bool        `json:"nx,omitempty"`
	XX                bool        `json:"xx,omitempty"`
	Async             bool        `json:"async,omitempty"`
}

type WritedownWithLockRequest

type WritedownWithLockRequest struct {
	Key         string      `json:"key"`
	ID          interface{} `json:"id"`
	Expiration  int64       `json:"expiration_seconds,omitempty"`
	LockTimeout int64       `json:"lock_timeout_seconds,omitempty"`
}

type WritedownWithVersionRequest

type WritedownWithVersionRequest[T any] struct {
	Key        string `json:"key"`
	Data       *T     `json:"data"`
	Version    int64  `json:"version"`
	Expiration int64  `json:"expiration_seconds,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL