Documentation
¶
Index ¶
- Constants
- Variables
- func AddModels(v ...interface{})
- func AutoDelete(db *gorm.DB, bus interface{})
- func BatchDeleteFromTop(db *gorm.DB, model interface{}, count int)
- func CreateIfNotExists(db *gorm.DB, bean interface{}, query interface{}, args ...interface{}) error
- func FindRecords[T any](val T, call func(tx *gorm.DB) *gorm.DB, args ...interface{}) (sliceResult []T, err error)
- func FindRecordsWithDB[T any](db *gorm.DB, val T, call func(tx *gorm.DB) *gorm.DB, args ...interface{}) (sliceResult []T, err error)
- func GetDBLoggerLevel(db *gorm.DB) logger.LogLevel
- func HDeleter(c *redis.Client, key string, args ...interface{}) func(fields ...string)
- func HScanCallback(c *redis.Client, key, match string, fn func(k, v string))
- func HasMigrateLockFile() bool
- func Md5bit16(b []byte) string
- func ModelColumns(model interface{}) (columns []string)
- func ModelTableName(model interface{}) string
- func NewDBLoggerSilent() logger.Interface
- func NewDBLoggerWithLevel(level logger.LogLevel) logger.Interface
- func NewDBLoggerWithLevelOut(level logger.LogLevel, logName string) logger.Interface
- func NewPlugin(cfg Gm2cConfig) gorm.Plugin
- func ParseModel(model interface{}) (s *schema.Schema, err error)
- func SetModelBus(bus interface{})
- func ValueIDUint64(val util.Map) (id uint64, ok bool)
- type BaseModel
- type CCStore
- type CacheStore
- type CreateClause
- type CurdParams
- type DBOption
- type FindByID
- type FindParams
- type FindResult
- type FreeStore
- type Gm2cConfig
- type Gm2cPlugin
- type Gm2cStats
- type GormDB
- func (g *GormDB) CheckDBNil() (err error)
- func (g *GormDB) ClearBeanCache(bean any) error
- func (g *GormDB) ClearBeanCacheByID(model, id any) error
- func (g *GormDB) ClearTableCache(table string)
- func (g *GormDB) Close() (err error)
- func (g *GormDB) CreateDB()
- func (g *GormDB) DBInitializationWithViper(v *viper.Viper, modelBus interface{}, args ...interface{})
- func (g *GormDB) DropDB()
- func (g *GormDB) GetFindModel(table string) (interface{}, error)
- func (g *GormDB) GetModel(table string) (interface{}, error)
- func (g *GormDB) Initialize(opt *DBOption) *GormDB
- func (g *GormDB) MigrateModels(v ...interface{})
- func (g *GormDB) ModelByTableName(tableName string) (model interface{}, err error)
- func (g *GormDB) ModelTableName(model interface{}) (name string)
- func (g *GormDB) Models() map[string]interface{}
- func (g *GormDB) Opt() *DBOption
- func (g *GormDB) RegModel(model interface{})
- func (g *GormDB) RegModelBus(bus interface{})
- func (g *GormDB) RegViewModel(model interface{})
- func (g *GormDB) SetOpt(opt *DBOption)
- func (g *GormDB) ViewModels() map[string]interface{}
- type ImplResultAfterFind
- type ItfModelInitializer
- type ItfStorageCache
- type Pagination
- type RedisOptions
- type RedisStore
- func (r *RedisStore) ClearAll()
- func (r *RedisStore) Del(key string)
- func (r *RedisStore) DropPrefix(prefix ...string)
- func (r *RedisStore) Get(key string) (data []byte, ok bool)
- func (r *RedisStore) GetKeyFieldOrNot(key string) (k, f string, y bool)
- func (r *RedisStore) ScanKeys(matchParam string) []string
- func (r *RedisStore) Set(key string, data []byte, ttl ...int64)
- func (r *RedisStore) StoreGC(prefix string)
- type RedisStoreOption
- type RedisV9
- type RedisValue
- type Time
- type UpdateClause
- type Version
- func (v *Version) CreateClauses(field *schema.Field) []clause.Interface
- func (v *Version) MarshalJSON() ([]byte, error)
- func (v *Version) Scan(value interface{}) error
- func (v *Version) UnmarshalJSON(bytes []byte) error
- func (v *Version) UpdateClauses(field *schema.Field) []clause.Interface
- func (v *Version) Value() (driver.Value, error)
- type WrapperDBFunc
Constants ¶
const NoCache = gormcache.NoCache
NoCache disables this plugin for the current GORM statement. Value forwards to gormcache.NoCache so callers using db.Set(mdb.NoCache, true) keep working against the underlying plugin.
Variables ¶
var ( DB = NewGormDB() ErrRecordExists = errors.New("record exists") )
var Gm2cCacheStat = Gm2cStats{}
Gm2cCacheStat exposes hit/miss counters as a package var so callers may read them via atomic.LoadUint64(&mdb.Gm2cCacheStat.Hit).
var Rdb = new(RedisV9)
var SeparatorColon = ":"
Functions ¶
func AutoDelete ¶
func BatchDeleteFromTop ¶
BatchDeleteFromTop eachCount = 1000; count: count of delete rows , order by id asc
func CreateIfNotExists ¶
func FindRecords ¶
func FindRecordsWithDB ¶
func FindRecordsWithDB[T any](db *gorm.DB, val T, call func(tx *gorm.DB) *gorm.DB, args ...interface{}) (sliceResult []T, err error)
FindRecordsWithDB ...
func HasMigrateLockFile ¶
func HasMigrateLockFile() bool
func ModelColumns ¶
func ModelColumns(model interface{}) (columns []string)
func ModelTableName ¶
func ModelTableName(model interface{}) string
func NewDBLoggerSilent ¶
func NewDBLoggerWithLevelOut ¶
func NewPlugin ¶
func NewPlugin(cfg Gm2cConfig) gorm.Plugin
NewPlugin builds a gorm.Plugin that wraps a gormcache.Plugin configured from cfg. The returned plugin is registered under cfg.Prefix so that the caller's *Gm2cConfig can later locate it for invalidations.
func ParseModel ¶
func SetModelBus ¶
func SetModelBus(bus interface{})
Types ¶
type BaseModel ¶
type BaseModel struct {
ID int `json:"id,omitempty" gorm:"primaryKey;autoIncrement:true;autoIncrementIncrement:1;"`
CreatedAt *Time `json:"created_at,omitempty" gorm:"notnull;default:CURRENT_TIMESTAMP;"`
UpdatedAt *Time `json:"updated_at,omitempty" gorm:"notnull;"`
Version *Version `json:"version,omitempty" gorm:"notnull;default:1;comment:版本号;"`
}
type CCStore ¶
type CCStore struct {
// contains filtered or unexported fields
}
func GetCCacheStore ¶
func GetCCacheStore() *CCStore
func NewCCacheStore ¶
func NewCCacheStore() *CCStore
func (*CCStore) DropPrefix ¶
type CacheStore ¶
type CacheStore interface {
ItfStorageCache
}
CacheStore is the cache backend accepted by Gm2cConfig. It is intentionally an independent interface (not a type alias) so the mdb package keeps a stable surface. Because its method set matches gormcache.Store structurally, a CacheStore value is assignable to a gormcache.Store parameter without conversion.
type CreateClause ¶
func (CreateClause) Build ¶
func (v CreateClause) Build(clause.Builder)
func (CreateClause) MergeClause ¶
func (v CreateClause) MergeClause(*clause.Clause)
func (CreateClause) ModifyStatement ¶
func (v CreateClause) ModifyStatement(stmt *gorm.Statement)
func (CreateClause) Name ¶
func (v CreateClause) Name() string
type CurdParams ¶
type CurdParams struct {
Table string `json:"table,omitempty"`
Values util.Map `json:"values,omitempty"`
Where string `json:"where,omitempty"`
Model interface{} `json:"-"`
BeforeCall func(bean interface{})
Check func(bean interface{}) (err error)
CheckBeforeUpdate func(oldVal, newVal interface{}) (err error)
DBWrapper WrapperDBFunc
AfterCall func(bean interface{}) (err error)
}
func (*CurdParams) AddUserIDCondition ¶
func (c *CurdParams) AddUserIDCondition(bean interface{}, userID uint64)
func (*CurdParams) AddWhereCondition ¶
func (c *CurdParams) AddWhereCondition(where string)
AddWhereCondition ... 添加where条件
type DBOption ¶
type DBOption struct {
Type string `json:"type"`
Host string `json:"host"`
Port string `json:"port"`
User string `json:"user"`
Pwd string `json:"pwd"`
DB string `json:"db"`
SkipCache bool `json:"skip_cache"`
CacheType string `json:"cache_type"`
Logger logger.Interface `json:"-"`
MaxIdleConns int `json:"max_idle_conns"`
MaxOpenConns int `json:"max_open_conns"`
MaxIdleTimeSeconds int64 `json:"max_idle_time_seconds"`
MaxLifetimeSeconds int64 `json:"max_lifetime_seconds"`
SkipCreateDB bool `json:"skipCreateDb,omitempty"`
Gm2cConfig *Gm2cConfig `json:"gm2c_config,omitempty"`
}
func NewOptionWithViper ¶
type FindByID ¶
type FindByID struct {
TableName string `json:"table_name,omitempty"`
ID uint64 `json:"id,omitempty"`
Condition string `json:"condition,omitempty"`
Dest interface{} `json:"-"`
}
func (*FindByID) AppendEqCondition ¶
AppendEqCondition ...
type FindParams ¶
type FindParams struct {
Table string `json:"table,omitempty"`
Condition string `json:"condition,omitempty"`
Order string `json:"order,omitempty"`
PageIndex int `json:"page_index,omitempty"`
PageSize int `json:"page_size,omitempty"`
Dest interface{} `json:"-"`
}
func (*FindParams) AppendEqCondition ¶
func (f *FindParams) AppendEqCondition(str string)
AppendEqCondition ...
func (*FindParams) FindResultWithModel ¶
func (f *FindParams) FindResultWithModel(tx *gorm.DB) (result *FindResult, err error)
FindResultWithModel get data
type FindResult ¶
type FindResult struct {
// Data
Data interface{} `json:"data,omitempty"`
// Pagination
Pagination *Pagination `json:"pagination,omitempty"`
// Extra
Extra interface{} `json:"extra,omitempty"`
}
type FreeStore ¶
type FreeStore struct {
// contains filtered or unexported fields
}
func GetFreeCacheStore ¶
func GetFreeCacheStore() *FreeStore
func NewFreeCacheStore ¶
func NewFreeCacheStore() *FreeStore
func (*FreeStore) DropPrefix ¶
type Gm2cConfig ¶
type Gm2cConfig struct {
Skip bool
Prefix string
TTL int64 // seconds
Store CacheStore
}
Gm2cConfig configures the single-record cache plugin. Field names, order, and exported types remain stable for external callers.
func (*Gm2cConfig) ClearBean ¶
func (g *Gm2cConfig) ClearBean(bean interface{}) error
ClearBean invalidates the cache entry for the single row identified by bean.
func (*Gm2cConfig) ClearBeanByID ¶
func (g *Gm2cConfig) ClearBeanByID(model, id interface{}) error
ClearBeanByID invalidates the cache entry for table(model) row with primary key id.
func (*Gm2cConfig) ClearTable ¶
func (g *Gm2cConfig) ClearTable(table string)
ClearTable invalidates every cached row for table.
type Gm2cPlugin ¶
type Gm2cPlugin struct {
// contains filtered or unexported fields
}
Gm2cPlugin adapts a gormcache.Plugin to the legacy mdb plugin shape.
func (*Gm2cPlugin) Initialize ¶
func (p *Gm2cPlugin) Initialize(db *gorm.DB) error
Initialize installs the underlying gormcache.Plugin and registers a post-query callback that mirrors gormcache counters into Gm2cCacheStat.
func (*Gm2cPlugin) Name ¶
func (p *Gm2cPlugin) Name() string
Name preserves the legacy plugin name for backwards compatibility with any caller that inspects db.Plugins["single-record-cache"].
type GormDB ¶
func (*GormDB) CheckDBNil ¶
func (*GormDB) ClearBeanCache ¶
func (*GormDB) ClearBeanCacheByID ¶
func (*GormDB) ClearTableCache ¶
func (*GormDB) DBInitializationWithViper ¶
func (*GormDB) GetFindModel ¶
GetFindModel ...
func (*GormDB) ModelByTableName ¶
ModelByTableName ...
func (*GormDB) ModelTableName ¶
ModelTableName ...
func (*GormDB) ViewModels ¶
type ImplResultAfterFind ¶
type ItfModelInitializer ¶
type ItfStorageCache ¶
type Pagination ¶
type RedisOptions ¶
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
func GetRedisStore ¶
func GetRedisStore(c *redis.Client, opts ...*RedisStoreOption) *RedisStore
func NewRedisStore ¶
func NewRedisStore(c *redis.Client, opts ...*RedisStoreOption) *RedisStore
func (*RedisStore) ClearAll ¶
func (r *RedisStore) ClearAll()
func (*RedisStore) Del ¶
func (r *RedisStore) Del(key string)
func (*RedisStore) DropPrefix ¶
func (r *RedisStore) DropPrefix(prefix ...string)
func (*RedisStore) GetKeyFieldOrNot ¶
func (r *RedisStore) GetKeyFieldOrNot(key string) (k, f string, y bool)
func (*RedisStore) ScanKeys ¶
func (r *RedisStore) ScanKeys(matchParam string) []string
func (*RedisStore) StoreGC ¶
func (r *RedisStore) StoreGC(prefix string)
type RedisStoreOption ¶
type RedisStoreOption struct {
Separator string
}
type RedisV9 ¶
type RedisV9 struct {
// contains filtered or unexported fields
}
func (*RedisV9) Initialize ¶
func (r *RedisV9) Initialize(opt *RedisOptions) *RedisV9
func (*RedisV9) Opt ¶
func (r *RedisV9) Opt() *RedisOptions
type RedisValue ¶
type RedisValue struct {
Val util.RawMessage `json:"val"`
ExpireAt int64 `json:"expire_at"`
}
type UpdateClause ¶
func (UpdateClause) Build ¶
func (v UpdateClause) Build(clause.Builder)
func (UpdateClause) MergeClause ¶
func (v UpdateClause) MergeClause(*clause.Clause)
func (UpdateClause) ModifyStatement ¶
func (v UpdateClause) ModifyStatement(stmt *gorm.Statement)
func (UpdateClause) Name ¶
func (v UpdateClause) Name() string