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
func New(name string, x _T, opt ...persist.Option) *Var_N {
func New(name string, x _T, ps ...bool) *Var_N {
gob.Register(x)
ptr := persist.New(name, x, opt...)
ptr := persist.New(name, x, ps...)
ret := (*Var_N)(unsafe.Pointer(ptr))
return ret
}

View File

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

View File

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