By default, we will use a persistent cache for the client cache. This will allow us to keep the cache across restarts of the server and it will also reduce the amount of time it takes to start the server while reducing the amount of memory used. This change also includes a validation of the cache schema version. If the schema version is different from the one expected, the cache will be reset/recreated. (cherry picked from commit cf8a51f3834033b7f986a9a7e091ab31c4328415)
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package db
|
|
|
|
import (
|
|
"github.com/hashicorp/go-dbw"
|
|
"github.com/hashicorp/go-hclog"
|
|
)
|
|
|
|
type options struct {
|
|
withSchemaVersion string
|
|
withDebug bool
|
|
withUrl string
|
|
withDbType dbw.DbType
|
|
withGormFormatter hclog.Logger
|
|
}
|
|
|
|
// Option - how options are passed as args
|
|
type Option func(*options) error
|
|
|
|
func getDefaultOptions() options {
|
|
return options{
|
|
withDbType: dbw.Sqlite,
|
|
}
|
|
}
|
|
|
|
func getOpts(opt ...Option) (options, error) {
|
|
opts := getDefaultOptions()
|
|
|
|
for _, o := range opt {
|
|
if err := o(&opts); err != nil {
|
|
return opts, err
|
|
}
|
|
}
|
|
return opts, nil
|
|
}
|
|
|
|
func WithGormFormatter(logger hclog.Logger) Option {
|
|
return func(o *options) error {
|
|
o.withGormFormatter = logger
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// withTestValidSchemaVersion provides optional valid schema version for testing
|
|
// purposes. This is used to simulate a schema version that is valid/invalid.
|
|
func withTestValidSchemaVersion(useVersion string) Option {
|
|
return func(o *options) error {
|
|
o.withSchemaVersion = useVersion
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// WithUrls provides optional url
|
|
func WithUrl(url string) Option {
|
|
return func(o *options) error {
|
|
o.withUrl = url
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// WithDebug provides an optional debug flag.
|
|
func WithDebug(debug bool) Option {
|
|
return func(o *options) error {
|
|
o.withDebug = debug
|
|
return nil
|
|
}
|
|
}
|