odb

package module
v0.3.24 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: BSD-2-Clause Imports: 13 Imported by: 1

README

odb

Documentation

Overview

Copyright 2022 Guan Jianchang. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	INSERT_TAG_DEFAULT     = "i"
	SELECT_TAG_DEFAULT     = "s"
	SELECT_KEY_TAG_DEFAULT = "sk"
	UPDATE_TAG_DEFAULT     = "u"
	UPDATE_KEY_TAG_DEFAULT = "uk"
)
View Source
const (
	INIT_REUSE_COUNT = 10
	MAX_REUSE_COUNT  = 100
)
View Source
const (
	CACHE_FIELD_UPDATE_TIME = "cache_update_time"
)

Variables

View Source
var (
	ErrCacheHasExit         = errors.New("has exit")
	ErrCacheSubscribeFailed = errors.New("subscribe failed")
)
View Source
var (
	ErrNoDataWorker  = errors.New("no data worker")
	ErrNoDbDriver    = errors.New("no db driver")
	ErrAffectRowZero = errors.New("affect row is zero")
)
View Source
var (
	ErrNoCache                   = errors.New("no cache")
	ErrNoDb                      = errors.New("no db")
	ErrWorkerObjectIsNil         = errors.New("object is nil")
	ErrWorkerRowObjIsNil         = errors.New("row object is nil")
	ErrWorkerInsertFieldNotFind  = errors.New("insert field not find")
	ErrWorkerReplaceFieldNotFind = errors.New("insert field and update field not find")
	ErrWorkerKeyFieldNotDefine   = errors.New("key field not define")
	ErrWorkerCacheNotExist       = errors.New("cache data no exist")
	ErrWorkerDBNotExist          = errors.New("db data no data")
	ErrWorkerFieldNotExist       = errors.New("some field not exist")
	ErrWorkerCacheKeyNotFind     = errors.New("cache key not define")
	ErrWorkerCacheKeyNotString   = errors.New("cache key not string")
	ErrWorkerDBCountUnknownErr   = errors.New("unknown error while count")
)
View Source
var (
	ErrMapperIsNil  = errors.New("mapper is nil")
	ErrNotRowObject = errors.New("not table row object")
)
View Source
var Builder = &builder{
	logger: yx.NewLogger("odb.Builder"),
}
View Source
var RowObjFactory = yx.NewObjectFactory()

Functions

func ExecTransaction added in v0.3.7

func ExecTransaction(tx *sql.Tx, query string, args ...interface{}) error

func FromCache added in v0.2.9

func FromCache(r DBTableRow, mapField2Val map[string]string) error

func GenRowObjRegisterFileByCfg

func GenRowObjRegisterFileByCfg(cfg *Config, regFilePath string, regPackName string)

func InsertTransaction added in v0.3.7

func InsertTransaction(tx *sql.Tx, query string, args ...interface{}) (int64, error)

func ToCache added in v0.2.9

func ToCache(r DBTableRow, fields []string) (map[string]interface{}, error)

Types

type BaseDBTableRow added in v0.2.9

type BaseDBTableRow struct {
}

func (*BaseDBTableRow) FromCache added in v0.2.9

func (r *BaseDBTableRow) FromCache(mapField2Val map[string]string) error

func (*BaseDBTableRow) ToCache added in v0.2.9

func (r *BaseDBTableRow) ToCache(fields []string) (map[string]interface{}, error)

type CacheConf

type CacheConf struct {
	Tag  string `json:"tag"`
	Addr string `json:"addr"`
	Pwd  string `json:"pwd"`
	Db   int    `json:"db"`
}

type CacheDriver

type CacheDriver struct {
	// contains filtered or unexported fields
}

func NewCacheDriver

func NewCacheDriver() *CacheDriver

func (*CacheDriver) BLPop

func (d *CacheDriver) BLPop(timeout time.Duration, keys ...string) ([]string, error)

func (*CacheDriver) Close

func (d *CacheDriver) Close() error

func (*CacheDriver) Del

func (d *CacheDriver) Del(keys ...string) (int64, error)

func (*CacheDriver) Exists

func (d *CacheDriver) Exists(keys ...string) (int64, error)

func (*CacheDriver) Expire

func (d *CacheDriver) Expire(key string, expiration time.Duration) (bool, error)

func (*CacheDriver) ExpireAt

func (d *CacheDriver) ExpireAt(key string, tm time.Time) (bool, error)

func (*CacheDriver) Get

func (d *CacheDriver) Get(key string) (string, error)

func (*CacheDriver) GetDB added in v0.3.6

func (d *CacheDriver) GetDB() *redis.Client

func (*CacheDriver) HDel

func (d *CacheDriver) HDel(key string, field string) (int64, error)

func (*CacheDriver) HExists

func (d *CacheDriver) HExists(key string, field string) (bool, error)

func (*CacheDriver) HGetAll

func (d *CacheDriver) HGetAll(key string) (map[string]string, error)

func (*CacheDriver) HMGet

func (d *CacheDriver) HMGet(key string, fields ...string) ([]interface{}, error)

func (*CacheDriver) HMSet

func (d *CacheDriver) HMSet(key string, fields map[string]interface{}) error

func (*CacheDriver) HSet

func (d *CacheDriver) HSet(key string, field string, value interface{}) (bool, error)

hash

func (*CacheDriver) HSetNX

func (d *CacheDriver) HSetNX(key string, field string, value interface{}) (bool, error)

func (*CacheDriver) Incr

func (d *CacheDriver) Incr(key string) (int64, error)

func (*CacheDriver) Keys

func (d *CacheDriver) Keys(pattern string) ([]string, error)

func (*CacheDriver) LLen

func (d *CacheDriver) LLen(key string) (int64, error)

func (*CacheDriver) LRange

func (d *CacheDriver) LRange(key string, start int64, stop int64) ([]string, error)

func (*CacheDriver) LRem

func (d *CacheDriver) LRem(key string, count int64, value interface{}) (int64, error)

func (*CacheDriver) LSet

func (d *CacheDriver) LSet(key string, index int64, value interface{}) error

func (*CacheDriver) MGet

func (d *CacheDriver) MGet(keys ...string) ([]interface{}, error)

func (*CacheDriver) MSet

func (d *CacheDriver) MSet(pairs ...interface{}) (string, error)

func (*CacheDriver) Open

func (d *CacheDriver) Open(addr string, pwd string, db int) error

func (*CacheDriver) Persist

func (d *CacheDriver) Persist(key string) (bool, error)

func (*CacheDriver) Publish

func (d *CacheDriver) Publish(channel string, message interface{}) error

func (*CacheDriver) RPush

func (d *CacheDriver) RPush(key string, values ...interface{}) error

list

func (*CacheDriver) Set

func (d *CacheDriver) Set(key string, value interface{}, expiration time.Duration) error

func (*CacheDriver) SetNX

func (d *CacheDriver) SetNX(key string, value interface{}, expiration time.Duration) (bool, error)

func (*CacheDriver) Subscribe

func (d *CacheDriver) Subscribe(cb RedisSubscribeCb, channels ...string) error

func (*CacheDriver) TTL

func (d *CacheDriver) TTL(key string) (time.Duration, error)

type CacheSaver added in v0.3.23

type CacheSaver interface {
	SaveCache(updateKeys []string) error
}

type Cacheable

type Cacheable interface {
	FromCache(mapField2Val map[string]string) error
	ToCache(fields []string) (map[string]interface{}, error)
}

type Config

type Config struct {
	SaveIntv  int            `json:"save_interval"`
	ClearIntv int            `json:"clear_interval"`
	Storages  []*StorageConf `json:"storages"`
}

type CountRow added in v0.3.6

type CountRow struct {
	BaseDBTableRow

	Count int64 `db:"count"`
}

type DBConf

type DBConf struct {
	Tag     string `json:"tag"`
	Addr    string `json:"addr"`
	DbName  string `json:"db_name"`
	Acc     string `json:"acc"`
	Pwd     string `json:"pwd"`
	Charset string `json:"charset"`
}

type DBTableRow

type DBTableRow interface {
	Cacheable
}

type DataCenter

type DataCenter struct {
	// contains filtered or unexported fields
}

func NewDataCenter

func NewDataCenter() *DataCenter

func (*DataCenter) AddCacheDriver

func (c *DataCenter) AddCacheDriver(tag string, driver *CacheDriver)

func (*DataCenter) AddDbDriver

func (c *DataCenter) AddDbDriver(tag string, driver *DbDriver)

func (*DataCenter) AddWorker

func (c *DataCenter) AddWorker(tag string, w *DataWorker)

func (*DataCenter) BeginTransaction added in v0.3.7

func (c *DataCenter) BeginTransaction(dbTag string) (*sql.Tx, error)

func (*DataCenter) CloseAllCacheDriver

func (c *DataCenter) CloseAllCacheDriver()

func (*DataCenter) CloseAllDbDriver

func (c *DataCenter) CloseAllDbDriver()

func (*DataCenter) Count added in v0.3.7

func (c *DataCenter) Count(tag string, mapper interface{}, extraCond string) (int64, error)

func (*DataCenter) GetCacheDriver

func (c *DataCenter) GetCacheDriver(tag string) (*CacheDriver, bool)

func (*DataCenter) GetDbDriver

func (c *DataCenter) GetDbDriver(tag string) (*DbDriver, bool)

func (*DataCenter) GetWorker

func (c *DataCenter) GetWorker(tag string) (*DataWorker, bool)

func (*DataCenter) Insert added in v0.3.7

func (c *DataCenter) Insert(tag string, mapper interface{}, extraCond string) (int64, error)

func (*DataCenter) NameExec added in v0.3.8

func (c *DataCenter) NameExec(dbTag string, query string, mapper interface{}) error

func (*DataCenter) NameInsert added in v0.3.8

func (c *DataCenter) NameInsert(dbTag string, query string, mapper interface{}) (int64, error)

func (*DataCenter) NameQuery added in v0.3.8

func (c *DataCenter) NameQuery(dbTag string, rowReflectName string, query string, mapper interface{}) ([]DBTableRow, error)

func (*DataCenter) ReplaceToDb added in v0.3.7

func (c *DataCenter) ReplaceToDb(tag string, mapper interface{}, extraCond string) error

func (*DataCenter) SelectRowFromDb added in v0.3.7

func (c *DataCenter) SelectRowFromDb(tag string, mapper interface{}, extraCond string) (DBTableRow, error)

func (*DataCenter) SelectRowsFromDb added in v0.3.7

func (c *DataCenter) SelectRowsFromDb(tag string, mapper interface{}, extraCond string, limitCnt int) ([]DBTableRow, error)

func (*DataCenter) Start

func (c *DataCenter) Start(saveIntv time.Duration, clearExpireIntv time.Duration)

func (*DataCenter) Stop

func (c *DataCenter) Stop()

func (*DataCenter) UpdateToDb added in v0.3.7

func (c *DataCenter) UpdateToDb(tag string, mapper interface{}, extraCond string) error

type DataWorker

type DataWorker struct {
	// contains filtered or unexported fields
}

func NewDataWorker

func NewDataWorker(cacheDriver *CacheDriver, cacheKeyName string, dbDriver *DbDriver, tableName string) *DataWorker

func (*DataWorker) ClearExpireCaches

func (w *DataWorker) ClearExpireCaches() error

func (*DataWorker) Count added in v0.3.6

func (w *DataWorker) Count(mapper interface{}) (int64, error)

func (*DataWorker) CountEx added in v0.3.6

func (w *DataWorker) CountEx(mapper interface{}, extraCond string) (int64, error)

func (*DataWorker) DelCacheDataFields added in v0.3.17

func (w *DataWorker) DelCacheDataFields(key string, fields ...string) (int64, error)

func (*DataWorker) DelCacheDatas added in v0.3.17

func (w *DataWorker) DelCacheDatas(keys ...string) (int64, error)

func (*DataWorker) GetAllCacheKeys

func (w *DataWorker) GetAllCacheKeys() ([]string, error)

func (*DataWorker) GetCacheData

func (w *DataWorker) GetCacheData(obj Cacheable, key string, fields ...string) error

func (*DataWorker) GetCacheDataToMap

func (w *DataWorker) GetCacheDataToMap(key string, fields ...string) (map[string]string, error)

func (*DataWorker) GetDbDriver added in v0.3.2

func (w *DataWorker) GetDbDriver() (*DbDriver, bool)

func (*DataWorker) GetInsertSql added in v0.2.14

func (w *DataWorker) GetInsertSql() string

func (*DataWorker) GetReplaceSql added in v0.2.14

func (w *DataWorker) GetReplaceSql() string

func (*DataWorker) GetSelectSql added in v0.2.14

func (w *DataWorker) GetSelectSql() string

func (*DataWorker) GetTableName added in v0.2.13

func (w *DataWorker) GetTableName() string

func (*DataWorker) GetUpdateSql added in v0.2.14

func (w *DataWorker) GetUpdateSql() string

func (*DataWorker) HasCache

func (w *DataWorker) HasCache() bool

func (*DataWorker) HasDb

func (w *DataWorker) HasDb() bool

func (*DataWorker) Init

func (w *DataWorker) Init(reflectName string, insertTag string, selectTag string, selectKeyTag string, updateTag string, updateKeyTag string) error

func (*DataWorker) InsertToDb

func (w *DataWorker) InsertToDb(mapper interface{}) (int64, error)

func (*DataWorker) InsertToDbEx added in v0.3.6

func (w *DataWorker) InsertToDbEx(mapper interface{}, extraCond string) (int64, error)

func (*DataWorker) IsOpenAutoSave added in v0.2.9

func (w *DataWorker) IsOpenAutoSave() bool

func (*DataWorker) LoadFromCache

func (w *DataWorker) LoadFromCache(obj Cacheable, key string) error

func (*DataWorker) OpenAutoSave added in v0.2.9

func (w *DataWorker) OpenAutoSave()

func (*DataWorker) PreloadData

func (w *DataWorker) PreloadData(obj Cacheable, mapper interface{}) error

func (*DataWorker) ReplaceToDb added in v0.2.3

func (w *DataWorker) ReplaceToDb(mapper interface{}) (int64, error)

func (*DataWorker) ReplaceToDbEx added in v0.3.6

func (w *DataWorker) ReplaceToDbEx(mapper interface{}, extraCond string) (int64, error)

func (*DataWorker) SaveCaches

func (w *DataWorker) SaveCaches() error

func (*DataWorker) SelectFromDb

func (w *DataWorker) SelectFromDb(mapper interface{}) (DBTableRow, error)

func (*DataWorker) SelectFromDbEx added in v0.3.6

func (w *DataWorker) SelectFromDbEx(mapper interface{}, extraCond string) (DBTableRow, error)

func (*DataWorker) SelectRowsFromDb

func (w *DataWorker) SelectRowsFromDb(mapper interface{}, limitCnt int) ([]DBTableRow, error)

func (*DataWorker) SelectRowsFromDbEx added in v0.3.6

func (w *DataWorker) SelectRowsFromDbEx(mapper interface{}, extraCond string, limitCnt int) ([]DBTableRow, error)

func (*DataWorker) SetCacheData

func (w *DataWorker) SetCacheData(obj Cacheable, key string, fields ...string) error

func (*DataWorker) SetCacheDataByMap

func (w *DataWorker) SetCacheDataByMap(key string, mapField2Val map[string]interface{}, bMarkUpdate bool) error

func (*DataWorker) SetCacheExpire

func (w *DataWorker) SetCacheExpire(key string, expiration time.Duration) error

func (*DataWorker) SetCacheSaver added in v0.3.23

func (w *DataWorker) SetCacheSaver(saver CacheSaver)

func (*DataWorker) SetCacheUpdated

func (w *DataWorker) SetCacheUpdated(key string)

func (*DataWorker) SetInsertSql added in v0.3.11

func (w *DataWorker) SetInsertSql(insertSql string)

func (*DataWorker) SetReplaceSql added in v0.3.11

func (w *DataWorker) SetReplaceSql(replaceSql string)

func (*DataWorker) SetSelectSql added in v0.3.11

func (w *DataWorker) SetSelectSql(selectSql string)

func (*DataWorker) SetUpdateSql added in v0.3.11

func (w *DataWorker) SetUpdateSql(updateSql string)

func (*DataWorker) Start

func (w *DataWorker) Start(saveIntv time.Duration, clearExpireIntv time.Duration)

func (*DataWorker) Stop

func (w *DataWorker) Stop()

func (*DataWorker) UpdateToDb

func (w *DataWorker) UpdateToDb(mapper interface{}) (int64, error)

func (*DataWorker) UpdateToDbEx added in v0.3.6

func (w *DataWorker) UpdateToDbEx(mapper interface{}, extraCond string) (int64, error)

type DbDriver

type DbDriver struct {
	// contains filtered or unexported fields
}

func NewDbDriver

func NewDbDriver(rowObjFactory *yx.ObjectFactory) *DbDriver

func (*DbDriver) Begin added in v0.3.5

func (d *DbDriver) Begin() (*sql.Tx, error)

func (*DbDriver) Close

func (d *DbDriver) Close() error

func (*DbDriver) CreateTableRow

func (d *DbDriver) CreateTableRow(rowReflectName string) (DBTableRow, error)

func (*DbDriver) Exec

func (d *DbDriver) Exec(query string, args ...interface{}) (sql.Result, error)

func (*DbDriver) GetDB added in v0.3.6

func (d *DbDriver) GetDB() *sqlx.DB

func (*DbDriver) NameExec

func (d *DbDriver) NameExec(query string, mapper interface{}) (int64, error)

func (*DbDriver) NameInsert

func (d *DbDriver) NameInsert(query string, mapper interface{}) (int64, error)

func (*DbDriver) NameQuery

func (d *DbDriver) NameQuery(rowReflectName string, query string, mapper interface{}) ([]DBTableRow, error)

func (*DbDriver) Open

func (d *DbDriver) Open(userName string, pwd string, host string, database string, charset string) error

func (*DbDriver) Query added in v0.2.7

func (d *DbDriver) Query(sql string) ([]map[string]string, error)

func (*DbDriver) ReuseTableRow

func (d *DbDriver) ReuseTableRow(rowObj DBTableRow, rowReflectName string)

func (*DbDriver) Select

func (d *DbDriver) Select(dest interface{}, query string, args ...interface{}) error

type RedisSubscribeCb

type RedisSubscribeCb func(channel string, pattern string, payload string)

type StorageConf

type StorageConf struct {
	Cache   *CacheConf    `json:"cache"`
	Db      *DBConf       `json:"db"`
	Workers []*WorkerConf `json:"workers"`
}

type WorkerConf

type WorkerConf struct {
	// MapCacheField2DbField map[string]string `json:"field_map"`
	Tag            string `json:"tag"`
	IsOpenAutoSave bool   `json:"is_open_auto_save"`
	CacheKey       string `json:"cache_key"`
	TableName      string `json:"table_name"`
	RowObj         string `json:"row_obj"`
	InsertTag      string `json:"insert_tag"`
	SelectTag      string `json:"select_tag"`
	SelectKeyTag   string `json:"select_key_tag"`
	UpdateTag      string `json:"update_tag"`
	UpdateKeyTag   string `json:"update_key_tag"`
}

Jump to

Keyboard shortcuts

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