Functional options to persist.Init().

This commit is contained in:
Greg 2018-08-30 09:23:21 -04:00
parent 24d96151f4
commit 20c89c6e56
3 changed files with 48 additions and 40 deletions

View File

@ -12,9 +12,9 @@ import (
type Var_N persist.Var type Var_N persist.Var
func New(name string, x _T, opt ...persist.Option) *Var_N { func New(name string, x _T, ps ...bool) *Var_N {
gob.Register(x) gob.Register(x)
ptr := persist.New(name, x, opt...) ptr := persist.New(name, x, ps...)
ret := (*Var_N)(unsafe.Pointer(ptr)) ret := (*Var_N)(unsafe.Pointer(ptr))
return ret return ret
} }

View File

@ -25,16 +25,14 @@ type Var struct {
X interface{} X interface{}
Time time.Time Time time.Time
name string name string
opt Option permanent bool
} }
type encVar struct { type encVar struct {
X interface{} X interface{}
} }
type Option struct { type Option func(*Config)
Permanent bool
}
type Config struct { type Config struct {
configured bool configured bool
@ -43,42 +41,47 @@ type Config struct {
Tidy TidyConfig Tidy TidyConfig
} }
func Init(x ...Config) { func MaxVars(i int) Option {
return func(c *Config) {
c.MaxVars = i
}
}
func DBPath(s string) Option {
return func(c *Config) {
c.DBPath = s
}
}
func TidyInterval(d time.Duration) Option {
return func(c *Config) {
c.Tidy.Interval = d
}
}
func Init(confs ...Option) {
mux.Lock() mux.Lock()
c := Config { tc := TidyConfig {
Interval: 10 * time.Second,
}
conf = Config { // default config
configured: true, configured: true,
MaxVars: 64, // default config MaxVars: 64,
DBPath: "db", DBPath: "db",
} Tidy: tc,
conf = c
if len(x) > 0 {
c = x[0]
}
if c.MaxVars != 0 {
conf.MaxVars = c.MaxVars
}
if c.DBPath != "" {
conf.DBPath = c.DBPath
} }
if c.Tidy.Interval == 0 { for _,f := range(confs) {
conf.Tidy.Interval = 10 * time.Second f(&conf)
} else {
fmt.Println("Setting conf.Tidy.Interval to",c.Tidy.Interval)
conf.Tidy.Interval = c.Tidy.Interval
} }
if c.Tidy.Func == nil { if conf.Tidy.Func == nil {
conf.Tidy.Func = ExpireAfter(24 * time.Hour) conf.Tidy.Func = ExpireAfterFunc(24 * time.Hour)
} else {
fmt.Println("Setting conf.Tidy.Func")
conf.Tidy.Func = c.Tidy.Func
} }
mux.Unlock() mux.Unlock()
} }
func New(name string, x interface{},opt ...Option) *Var { func New(name string, x interface{},ps ...bool) *Var {
start() start()
mux.Lock() mux.Lock()
if vars == nil { if vars == nil {
@ -89,8 +92,8 @@ func New(name string, x interface{},opt ...Option) *Var {
ret = &Var{X: x, name: name} ret = &Var{X: x, name: name}
vars[name] = ret vars[name] = ret
} }
if len(opt) == 1 { if len(ps) > 1 {
ret.opt = opt[0] ret.permanent = ps[0]
} }
mux.Unlock() mux.Unlock()
err := ret.Load() err := ret.Load()
@ -450,7 +453,13 @@ type TidyConfig struct {
Func TidyFunc Func TidyFunc
} }
func ExpireAfter(exp time.Duration) TidyFunc { func ExpireAfter(exp time.Duration) Option {
return func(c *Config) {
c.Tidy.Func = ExpireAfterFunc(exp)
}
}
func ExpireAfterFunc(exp time.Duration) TidyFunc {
if exp > 0 { if exp > 0 {
exp = -1 * exp exp = -1 * exp
} }
@ -485,7 +494,7 @@ func tidyThread() {
} }
for { for {
for p := range loaded { for p := range loaded {
if p.opt.Permanent == false { if p.permanent == false {
lchan<- p.name lchan<- p.name
loaded<- p // put it back in the queue loaded<- p // put it back in the queue
} }

View File

@ -7,11 +7,10 @@ import (
) )
func main() { func main() {
tc := persist.TidyConfig{ persist.Init(
Interval: 2 * time.Second, persist.TidyInterval(2 * time.Second),
Func: persist.ExpireAfter(-5 * time.Second), persist.ExpireAfter(-5 * time.Second),
} )
persist.Init(persist.Config{Tidy: tc})
x := persistInt("x",0) x := persistInt("x",0)
persist.Commit() persist.Commit()