Skip to content

Commit

Permalink
add orm cache memory/redis (#29)
Browse files Browse the repository at this point in the history
Signed-off-by: dongjiang1989 <[email protected]>
  • Loading branch information
dongjiang1989 authored Apr 16, 2024
1 parent 6acd0f4 commit 929a39d
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 36 deletions.
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ go 1.19

require (
github.com/BurntSushi/toml v0.3.1
github.com/asjdf/gorm-cache v1.2.3
github.com/caarlos0/env/v10 v10.0.0
github.com/caarlos0/env/v6 v6.10.1
github.com/gin-gonic/gin v1.8.1
github.com/glebarez/sqlite v1.11.0
github.com/go-sql-driver/mysql v1.8.1
github.com/mattn/go-isatty v0.0.17
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.1
github.com/redis/go-redis/v9 v9.5.1
github.com/stretchr/testify v1.8.3
github.com/uber-go/tally v3.5.0+incompatible
github.com/vmihailenco/msgpack/v5 v5.3.5
Expand All @@ -31,11 +32,13 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/bluele/gcache v0.0.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-semver v0.2.0 // indirect
github.com/coreos/go-systemd/v22 v22.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
Expand All @@ -54,13 +57,16 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.9.5 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/karlseguin/ccache/v3 v3.0.3 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/m3db/prometheus_client_golang v1.12.8 // indirect
github.com/m3db/prometheus_client_model v0.2.1 // indirect
Expand All @@ -69,6 +75,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.34.0 // indirect
Expand Down
19 changes: 18 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,26 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/asjdf/gorm-cache v1.2.3 h1:h7GAMITzk6DdpOlAGlF0dUt25N8fK4R6zeQyO0pMqlA=
github.com/asjdf/gorm-cache v1.2.3/go.mod h1:PJjTYOCVblDX+GLbEndUqQKPxW+QibsIDO95EfPovBk=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bluele/gcache v0.0.2 h1:WcbfdXICg7G/DGBh1PFfcirkWOQV+v077yF1pSy3DGw=
github.com/bluele/gcache v0.0.2/go.mod h1:m15KV+ECjptwSPxKhOhQoAFQVtUFjTVkc3H8o0t/fp0=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
github.com/caarlos0/env/v6 v6.10.1 h1:t1mPSxNpei6M5yAeu1qtRdPAK29Nbcf/n3G7x+b3/II=
github.com/caarlos0/env/v6 v6.10.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand All @@ -69,6 +76,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
Expand Down Expand Up @@ -192,6 +201,10 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92Bcuy
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.5 h1:UImYN5qQ8tuGpGE16ZmjvcTtTw24zw1QAp/SlnNrZhI=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -219,6 +232,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/karlseguin/ccache/v3 v3.0.3 h1:cz+3tSdTrovp00xHPP3Y6ca/YuSl5kchhYG83wUPYN0=
github.com/karlseguin/ccache/v3 v3.0.3/go.mod h1:qxC372+Qn+IBj8Pe3KvGjHPj0sWwEF7AeZVhsNPZ6uY=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down Expand Up @@ -301,6 +316,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
GlobalCfg.GinConfig = GlobalCfg.GinConfig.DefaultConfig()
GlobalCfg.RateLimit = GlobalCfg.RateLimit.DefaultConfig()
GlobalCfg.Temporary = GlobalCfg.Temporary.DefaultConfig()
GlobalCfg.DBConfg = GlobalCfg.DBConfg.DefaultConfig()
GlobalCfg.DBConfig = GlobalCfg.DBConfig.DefaultConfig()

}

Expand All @@ -56,7 +56,7 @@ type Global struct {
GinConfig `toml:"gin"`
RateLimit `toml:"ratelimit"`
Temporary `toml:"temporary"`
DBConfg `toml:"database"`
DBConfig `toml:"database"`
}

func (g Global) TOML() string {
Expand All @@ -67,6 +67,6 @@ func (g Global) TOML() string {
g.GinConfig.TOML(),
g.RateLimit.TOML(),
g.Temporary.TOML(),
g.DBConfg.TOML(),
g.DBConfig.TOML(),
)
}
26 changes: 24 additions & 2 deletions pkg/config/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ func Test_global(t *testing.T) {
port = ""
## 数据库名
database = ""
## ca cert证书
ca_cert = ""`)
## args 参数
args = ""
[dbcache]
## orm_cache_type: redis, memory. 默认值为memory
orm_cache_type: memory
## orm_cache_model 模式: CacheDisable, CacheOnlyPrimary, CacheOnlySearch 和 CacheAll. 默认值为CacheOnlySearch
orm_cache_model = CacheOnlySearch
## orm_invalidate_when_update: 如果用户在数据库中更新/删除/创建某些内容,我们将使所有缓存数据无效以确保一致性. 如果true将会清理cache. 默认值为true
orm_invalidate_when_update = true
## orm_async_write: 如果 true,那么我们将以异步模式写入缓存. 默认值为true
orm_async_write = true
## orm_cache_ttl: CacheTTL 缓存 ttl,单位为 ms,其中 0 代表永远. 默认值为5000
orm_cache_ttl = 5000
## orm_disable_cache_penetration_protect: 如果为 true,那么我们不会缓存 nil 结果, 默认值为false
orm_disable_cache_penetration_protect = false
[dbcachecfg]
## cache addr
orm_cache_addr =
## cache username
orm_cache_username =
## cache password
orm_cache_password =
## cache db id
orm_cache_dbid = 0`)
}
144 changes: 129 additions & 15 deletions pkg/config/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,93 @@ import (
)

type DBTYPE string
type CACHETYPE string

const (
MYSQL DBTYPE = "mysql"
SQLITE3 DBTYPE = "sqlite3"
POSTGRES DBTYPE = "postgres"

CACHEREDIS CACHETYPE = "redis"
CACHEMEMORY CACHETYPE = "memory"
)

type CacheModel string

const (
ORMCacheDisable CacheModel = "CacheDisable"
ORMCacheOnlyPrimary CacheModel = "CacheOnlyPrimary"
ORMCacheOnlySearch CacheModel = "CacheOnlySearch"
ORMCacheAll CacheModel = "CacheAll"
)

// DBConfg represents a database configuration
type DBConfg struct {
DBType DBTYPE `toml:"db_type" json:"db_type" env:"ORM_DBTYPE"` // dbtype
User string `toml:"user" json:"user" env:"ORM_USER"` // user
Password string `toml:"password" json:"password" env:"ORM_PASSWORD"` // password
Host string `toml:"host" json:"host" env:"ORM_HOST"` // host
Port string `toml:"port" json:"port" env:"ORM_PORT"` // port
Database string `toml:"database" json:"database" env:"ORM_DATABASE"` // database
CaCert string `toml:"ca_cert" json:"ca_cert" env:"ORM_CACERT"` // cacert
func (c CacheModel) Number() int {
switch c {
case ORMCacheDisable:
return 0
case ORMCacheOnlyPrimary:
return 1
case ORMCacheOnlySearch:
return 2
case ORMCacheAll:
return 3
default:
return 3
}
}

type OrmCacheCfg struct {
// host:port address.
Addr string `toml:"orm_cache_addr" json:"orm_cache_addr" env:"ORM_CACHE_ADDR"`
// Use the specified Username to authenticate the current connection
// with one of the connections defined in the ACL list when connecting
// to a Redis 6.0 instance, or greater, that is using the Redis ACL system.
Username string `toml:"orm_cache_username" json:"orm_cache_username" env:"ORM_CACHE_USERNAME"`
// Optional password. Must match the password specified in the
// requirepass server configuration option (if connecting to a Redis 5.0 instance, or lower),
// or the User Password when connecting to a Redis 6.0 instance, or greater,
// that is using the Redis ACL system.
Password string `toml:"orm_cache_password" json:"orm_cache_password" env:"ORM_CACHE_PASSWORD"`
// Database to be selected after connecting to the server.
DB int `toml:"orm_cache_dbid" json:"orm_cache_dbid" env:"ORM_CACHE_DBID"`
}

type OrmCache struct {
// cache type: redis, memory
CacheType CACHETYPE `toml:"orm_cache_type" json:"orm_cache_type" env:"ORM_CACHE_TYPE" envDefault:"memory"`

// 4 kinds of cache model
CacheModel CacheModel `toml:"orm_cache_model" json:"orm_cache_model" env:"ORM_CACHE_MODEL" envDefault:"CacheOnlySearch"`

// if user update/delete/create something in DB, we invalidate all cached data to ensure consistency,
// else we do nothing to outdated cache.
InvalidateWhenUpdate bool `toml:"orm_invalidate_when_update" json:"orm_invalidate_when_update" env:"ORM_INVALIDATE_WHEN_UPDATE" envDefault:"true"`

// AsyncWrite if true, then we will write cache in async mode
AsyncWrite bool `toml:"orm_async_write" json:"orm_async_write" env:"ORM_ASYNC_WRITE" envDefault:"true"`

// CacheTTL cache ttl in ms, where 0 represents forever
CacheTTL int64 `toml:"orm_cache_ttl" json:"orm_cache_ttl" env:"ORM_CACHE_TTL" envDefault:"5000"`

// DisableCachePenetration if true, then we will not cache nil result
DisableCachePenetrationProtect bool `toml:"orm_disable_cache_penetration_protect" json:"orm_disable_cache_penetration_protect" env:"ORM_DISABLE_CACHE_PENETRATION_PROTECT" envDefault:"false"`

CacheCfg OrmCacheCfg `toml:"dbcachecfg"`
}

// DBConfig represents a database configuration
type DBConfig struct {
DBType DBTYPE `toml:"db_type" json:"db_type" env:"ORM_DBTYPE"` // dbtype
User string `toml:"user" json:"user" env:"ORM_USER"` // user
Password string `toml:"password" json:"password" env:"ORM_PASSWORD"` // password
Host string `toml:"host" json:"host" env:"ORM_HOST"` // host
Port string `toml:"port" json:"port" env:"ORM_PORT"` // port
Database string `toml:"database" json:"database" env:"ORM_DATABASE"` // database
Args string `toml:"orm_args" json:"orm_args" env:"ORM_ARGS"` // args
Cache OrmCache `toml:"dbcache"`
}

func (db DBConfg) TOML() string {
func (db DBConfig) TOML() string {
return fmt.Sprintf(`
[database]
## dbtype 模式: mysql/sqlite3/postgres
Expand All @@ -54,25 +122,71 @@ func (db DBConfg) TOML() string {
port = "%s"
## 数据库名
database = "%s"
## ca cert证书
ca_cert = "%s"`,
## args 参数
args = "%s"
[dbcache]
## orm_cache_type: redis, memory. 默认值为memory
orm_cache_type: %s
## orm_cache_model 模式: CacheDisable, CacheOnlyPrimary, CacheOnlySearch 和 CacheAll. 默认值为CacheOnlySearch
orm_cache_model = %s
## orm_invalidate_when_update: 如果用户在数据库中更新/删除/创建某些内容,我们将使所有缓存数据无效以确保一致性. 如果true将会清理cache. 默认值为true
orm_invalidate_when_update = %v
## orm_async_write: 如果 true,那么我们将以异步模式写入缓存. 默认值为true
orm_async_write = %v
## orm_cache_ttl: CacheTTL 缓存 ttl,单位为 ms,其中 0 代表永远. 默认值为5000
orm_cache_ttl = %v
## orm_disable_cache_penetration_protect: 如果为 true,那么我们不会缓存 nil 结果, 默认值为false
orm_disable_cache_penetration_protect = %v
[dbcachecfg]
## cache addr
orm_cache_addr = %v
## cache username
orm_cache_username = %v
## cache password
orm_cache_password = %v
## cache db id
orm_cache_dbid = %v`,
string(db.DBType),
db.User,
db.Password,
db.Host,
db.Port,
db.Database,
db.CaCert)
db.Args,
string(db.Cache.CacheType),
string(db.Cache.CacheModel),
db.Cache.InvalidateWhenUpdate,
db.Cache.AsyncWrite,
db.Cache.CacheTTL,
db.Cache.DisableCachePenetrationProtect,
db.Cache.CacheCfg.Addr,
db.Cache.CacheCfg.Username,
db.Cache.CacheCfg.Password,
db.Cache.CacheCfg.DB)
}

func (db DBConfg) DefaultConfig() DBConfg {
db = DBConfg{
func (db DBConfig) DefaultConfig() DBConfig {
db = DBConfig{
DBType: MYSQL,
User: "root",
Password: "root",
Host: "localhost",
Port: "3306",
Database: "test",
Cache: OrmCache{
CacheType: CACHETYPE("memory"),
CacheModel: ORMCacheOnlySearch,
InvalidateWhenUpdate: true,
CacheTTL: 5000,
AsyncWrite: true,
DisableCachePenetrationProtect: false,
CacheCfg: OrmCacheCfg{
Addr: "",
Username: "",
Password: "",
DB: 0,
},
},
}
return db
}
Loading

0 comments on commit 929a39d

Please sign in to comment.