Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e25e8eccc | |||
| 4f584726d6 | |||
| b9d144353e | |||
| 7e7fa16441 |
14
cache/mem/mem.go
vendored
14
cache/mem/mem.go
vendored
@@ -1,14 +0,0 @@
|
|||||||
package mem
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.apinb.com/bsm-sdk/core/vars"
|
|
||||||
"github.com/FishGoddess/cachego"
|
|
||||||
)
|
|
||||||
|
|
||||||
func New() cachego.Cache {
|
|
||||||
return cachego.NewCache(
|
|
||||||
cachego.WithGC(vars.MemGcDuration),
|
|
||||||
cachego.WithShardings(vars.MemShardings),
|
|
||||||
cachego.WithLFU(vars.MemLRUMaxNumber),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.apinb.com/bsm-sdk/core/database/sql"
|
"git.apinb.com/bsm-sdk/core/database/sql"
|
||||||
"git.apinb.com/bsm-sdk/core/types"
|
"git.apinb.com/bsm-sdk/core/types"
|
||||||
"git.apinb.com/bsm-sdk/core/vars"
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
@@ -12,6 +15,24 @@ var (
|
|||||||
MigrateTables []any
|
MigrateTables []any
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func NewDatabase(driver string, dsn []string, options *types.SqlOptions) (db *gorm.DB, err error) {
|
||||||
|
driver = strings.ToLower(driver)
|
||||||
|
switch driver {
|
||||||
|
case "mysql":
|
||||||
|
db, err = NewMysql(dsn, options)
|
||||||
|
case "postgres":
|
||||||
|
db, err = NewPostgres(dsn, options)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported database driver: %s", driver)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
// NewMysql 创建MySQL数据库服务
|
// NewMysql 创建MySQL数据库服务
|
||||||
func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err error) {
|
func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err error) {
|
||||||
//set connection default val.
|
//set connection default val.
|
||||||
|
|||||||
27
with/databases.go
Normal file
27
with/databases.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package with
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.apinb.com/bsm-sdk/core/conf"
|
||||||
|
"git.apinb.com/bsm-sdk/core/database"
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/types"
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Databases(cfg *conf.DBConf, db *gorm.DB, opts *types.SqlOptions) {
|
||||||
|
if cfg == nil || len(cfg.Source) == 0 {
|
||||||
|
panic("No Database Source Found !")
|
||||||
|
}
|
||||||
|
|
||||||
|
// print inform.
|
||||||
|
print.Info("[BSM - %s] Databases: %v", vars.ServiceKey, cfg)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
db, err = database.NewDatabase(cfg.Driver, cfg.Source, opts)
|
||||||
|
if err != nil {
|
||||||
|
print.Error("Database Init Failed !")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
54
with/etcd.go
Normal file
54
with/etcd.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package with
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.apinb.com/bsm-sdk/core/conf"
|
||||||
|
"git.apinb.com/bsm-sdk/core/errcode"
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
"go.etcd.io/etcd/client/pkg/v3/transport"
|
||||||
|
clientv3 "go.etcd.io/etcd/client/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Etcd(cfg *conf.EtcdConf, cli *clientv3.Client) {
|
||||||
|
if cfg == nil || len(cfg.Endpoints) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
etcdCfg := clientv3.Config{
|
||||||
|
Endpoints: cfg.Endpoints,
|
||||||
|
DialTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Passwd != nil {
|
||||||
|
etcdCfg.Username = cfg.Passwd.Account
|
||||||
|
etcdCfg.Password = cfg.Passwd.Password
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.TLS != nil {
|
||||||
|
tlsInfo := transport.TLSInfo{
|
||||||
|
TrustedCAFile: cfg.TLS.CaFile,
|
||||||
|
CertFile: cfg.TLS.CertFile,
|
||||||
|
KeyFile: cfg.TLS.KeyFile,
|
||||||
|
}
|
||||||
|
tlsConfig, err := tlsInfo.ClientConfig()
|
||||||
|
if err != nil {
|
||||||
|
print.Error(errcode.ErrEtcd.Error())
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
etcdCfg.TLS = tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
cli, err = clientv3.New(etcdCfg)
|
||||||
|
if err != nil {
|
||||||
|
print.Error(errcode.ErrEtcd.Error())
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// print inform.
|
||||||
|
print.Info("[BSM - %s] Service Center: %v", vars.ServiceKey, cfg.Endpoints)
|
||||||
|
return
|
||||||
|
}
|
||||||
39
with/memory.go
Normal file
39
with/memory.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package with
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
"github.com/allegro/bigcache/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Memory(cli *bigcache.BigCache, opts *bigcache.Config) {
|
||||||
|
config := bigcache.Config{
|
||||||
|
Shards: 1024,
|
||||||
|
LifeWindow: 10 * time.Minute,
|
||||||
|
CleanWindow: 5 * time.Minute,
|
||||||
|
MaxEntriesInWindow: 1000 * 10 * 60,
|
||||||
|
MaxEntrySize: 500,
|
||||||
|
Verbose: true,
|
||||||
|
HardMaxCacheSize: 8192,
|
||||||
|
OnRemove: nil,
|
||||||
|
OnRemoveWithReason: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts != nil {
|
||||||
|
config = *opts
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
cli, err = bigcache.New(context.Background(), config)
|
||||||
|
if err != nil {
|
||||||
|
print.Error("Memory Cache Fatal Error")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonBytes, _ := json.Marshal(config)
|
||||||
|
print.Success("[BSM - %s] Memory Cache: %s", vars.ServiceKey, jsonBytes)
|
||||||
|
}
|
||||||
16
with/redis.go
Normal file
16
with/redis.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package with
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RedisCache(cfg string, cli *redis.RedisClient) {
|
||||||
|
if cfg != "" {
|
||||||
|
cli = redis.New(cfg, vars.ServiceKey)
|
||||||
|
|
||||||
|
// print inform.
|
||||||
|
print.Info("[BSM - %s] Cache: %s, DBIndex: %d", vars.ServiceKey, cfg, cli.DB)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user