mongo

package
v6.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2019 License: Apache-2.0 Imports: 12 Imported by: 22

README

mongo

A mongodb ORM(Object Role Modeling) package with redis cache.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = mgo.ErrNotFound
	NewObjectId = bson.NewObjectId
)
View Source
var ErrCacheNil = errors.New("*DB.Cache (redis) is nil")

ErrCacheNil error: *DB.Cache (redis) is nil

Functions

This section is empty.

Types

type CacheKey

type CacheKey struct {
	Key         string
	FieldValues []interface{}
	// contains filtered or unexported fields
}

CacheKey cache key and values corresponding to primary keys

type Cacheable

type Cacheable interface {
	TableName() string
}

Cacheable the interface that can use cache. It must be orm-struct.

type CacheableDB

type CacheableDB struct {
	*DB
	// contains filtered or unexported fields
}

CacheableDB cacheable DB handle

func (*CacheableDB) CacheGet

func (c *CacheableDB) CacheGet(destStructPtr Cacheable, fields ...string) error

CacheGet selects one row by primary key. Priority from the read cache. Note:

If the cache does not exist, then write the cache;
@destStructPtr must be a *struct type;
If fields is empty, auto-use primary fields.

func (*CacheableDB) CreateCacheKey

func (c *CacheableDB) CreateCacheKey(structPtr Cacheable, fields ...string) (CacheKey, error)

CreateCacheKey creates cache key and fields' values. Note: if fields is empty, auto-use primary fields.

func (*CacheableDB) CreateCacheKeyByFields

func (c *CacheableDB) CreateCacheKeyByFields(fields []string, values []interface{}) (string, error)

CreateCacheKeyByFields creates the cache key string by specified fields and values.

func (*CacheableDB) CreateGetQuery

func (c *CacheableDB) CreateGetQuery(values []interface{}, whereFields ...string) M

func (*CacheableDB) DeleteCache

func (c *CacheableDB) DeleteCache(srcStructPtr Cacheable, fields ...string) error

DeleteCache deletes one row form cache by primary key. Note:

@destStructPtr must be a *struct type;
If fields is empty, auto-use primary fields.

func (*CacheableDB) PutCache

func (c *CacheableDB) PutCache(srcStructPtr Cacheable, fields ...string) error

PutCache caches one row by primary key. Note:

@destStructPtr must be a *struct type;
If fields is empty, auto-use primary fields.

func (*CacheableDB) WitchCollection

func (c *CacheableDB) WitchCollection(s func(*Collection) error) error

Common method

type Collection

type Collection = mgo.Collection

type Config

type Config struct {
	Addrs    []string      `yaml:"addrs"`
	Timeout  time.Duration `yaml:"timeout"`
	Database string        `yaml:"database"`
	// Username and Password inform the credentials for the initial authentication
	// done on the database defined by the Source field. See Session.Login.
	Username string `yaml:"username"`
	Password string `yaml:"passward"`
	// PoolLimit defines the per-server socket pool limit. Defaults to 4096.
	// See Session.SetPoolLimit for details.
	PoolLimit int `yaml:"pool_limit"`
	// NoCache whether to disable cache
	NoCache bool `yaml:"no_cache"`
	// contains filtered or unexported fields
}

Config mongodb setting

func NewConfig

func NewConfig() *Config

NewConfig creates a default config.

func ReadConfig

func ReadConfig(configSection string) (*Config, error)

ReadConfig gets a mongodb db config form yaml.

func (*Config) Reload

func (cfg *Config) Reload(bind cfgo.BindFunc) error

Reload sync automatically config from config file.

func (*Config) Source

func (mgoConfig *Config) Source() *mgo.DialInfo

Set config

type DB

type DB struct {
	*mgo.Session
	Cache *redis.Client
	// contains filtered or unexported fields
}

DB is a wrapper around sqlx.DB and redis.Client.

func (*DB) GetCacheableDB

func (d *DB) GetCacheableDB(tableName string) (*CacheableDB, error)

GetCacheableDB returns the specified *CacheableDB

func (*DB) RegCacheableDB

func (d *DB) RegCacheableDB(ormStructPtr Cacheable, cacheExpiration time.Duration) (*CacheableDB, error)

RegCacheableDB registers a cacheable table.

type M

type M = bson.M

GO 1.9 alias

type ObjectId

type ObjectId = bson.ObjectId

type PreDB

type PreDB struct {
	*DB
	// contains filtered or unexported fields
}

PreDB preset *DB

func NewPreDB

func NewPreDB() *PreDB

NewPreDB creates a unconnected *DB

func (*PreDB) Init

func (p *PreDB) Init(dbConfig *Config, redisConfig *redis.Config) (err error)

func (*PreDB) Init2

func (p *PreDB) Init2(dbConfig *Config, redisClient *redis.Client) (err error)

Init initialize *DB.

func (*PreDB) RegCacheableDB

func (p *PreDB) RegCacheableDB(ormStructPtr Cacheable, cacheExpiration time.Duration) (*CacheableDB, error)

RegCacheableDB registers a cacheable table.

Jump to

Keyboard shortcuts

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