Skip to content

Commit

Permalink
feat(provider): Badger singleton support multiple directives (#194)
Browse files Browse the repository at this point in the history
* feat(provider): Setup Badger singleton to support multiple Caddy directives

* Review feedbacks treatment
  • Loading branch information
darkweak authored Mar 18, 2022
1 parent ce6ab9e commit 04801fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cache/providers/badgerProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Badger struct {
stale time.Duration
}

var enabledBadgerInstances = make(map[string]*Badger)

// BadgerConnectionFactory function create new Badger instance
func BadgerConnectionFactory(c t.AbstractConfigurationInterface) (*Badger, error) {
dc := c.GetDefaultCache()
Expand All @@ -37,12 +39,22 @@ func BadgerConnectionFactory(c t.AbstractConfigurationInterface) (*Badger, error
} else {
badgerOptions = badgerOptions.WithInMemory(true)
}

uid := badgerOptions.Dir + badgerOptions.ValueDir
if i, ok := enabledBadgerInstances[uid]; ok {
return i, nil
}

db, e := badger.Open(badgerOptions)

if e != nil {
fmt.Println("Impossible to open the Badger DB.", e)
}

return &Badger{DB: db, stale: dc.GetStale()}, nil
i := &Badger{DB: db, stale: dc.GetStale()}
enabledBadgerInstances[uid] = i

return i, nil
}

// ListKeys method returns the list of existing keys
Expand Down
9 changes: 9 additions & 0 deletions plugins/caddy/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
prometheus
souin
}
badger {
configuration {
Dir /tmp/badger
ValueDir match2
ValueLogFileSize 16777216
MemTableSize 4194304
ValueThreshold 524288
}
}
regex {
exclude /test2.*
}
Expand Down

0 comments on commit 04801fe

Please sign in to comment.