storj/cmd/overlay/cache.go

53 lines
1.3 KiB
Go
Raw Normal View History

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"net/url"
"go.uber.org/zap"
"storj.io/storj/pkg/overlay"
2018-11-16 16:31:14 +00:00
"storj.io/storj/storage"
"storj.io/storj/storage/boltdb"
"storj.io/storj/storage/redis"
"storj.io/storj/storage/storelogger"
)
type cacheConfig struct {
NodesPath string `help:"the path to a JSON file containing an object with IP keys and nodeID values"`
DatabaseURL string `help:"the database connection string to use"`
}
func (c cacheConfig) open() (*overlay.Cache, error) {
dburl, err := url.Parse(c.DatabaseURL)
if err != nil {
return nil, Error.Wrap(err)
}
2018-11-16 16:31:14 +00:00
var db storage.KeyValueStore
switch dburl.Scheme {
case "bolt":
2018-11-16 16:31:14 +00:00
db, err = boltdb.New(dburl.Path, overlay.OverlayBucket)
if err != nil {
2018-11-16 16:31:14 +00:00
return nil, Error.New("invalid overlay cache database: %s", err)
}
zap.S().Info("Starting overlay cache with BoltDB")
case "redis":
2018-11-16 16:31:14 +00:00
db, err = redis.NewClientFrom(c.DatabaseURL)
if err != nil {
2018-11-16 16:31:14 +00:00
return nil, Error.New("invalid overlay cache database: %s", err)
}
zap.S().Info("Starting overlay cache with Redis")
default:
return nil, Error.New("database scheme not supported: %s", dburl.Scheme)
}
2018-11-16 16:31:14 +00:00
// add logger
db = storelogger.New(zap.L(), db)
return overlay.NewOverlayCache(db, nil, nil), nil
}