Add redis
parent
18096443d7
commit
6ac77d247e
11
db.go
11
db.go
|
|
@ -2,6 +2,7 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DB interface {
|
type DB interface {
|
||||||
|
|
@ -20,6 +21,8 @@ func New(key Type, params ...string) (db DB, err error) {
|
||||||
}()
|
}()
|
||||||
err = ErrNotImpl
|
err = ErrNotImpl
|
||||||
switch key {
|
switch key {
|
||||||
|
case REDIS:
|
||||||
|
db, err = NewRedis(params[0], params[1], params[2])
|
||||||
case MAP:
|
case MAP:
|
||||||
db = NewMap()
|
db = NewMap()
|
||||||
err = nil
|
err = nil
|
||||||
|
|
@ -44,7 +47,13 @@ func New(key Type, params ...string) (db DB, err error) {
|
||||||
func resolveNamespace(ns []string) string {
|
func resolveNamespace(ns []string) string {
|
||||||
namespace := DefaultNamespace
|
namespace := DefaultNamespace
|
||||||
if len(ns) > 0 {
|
if len(ns) > 0 {
|
||||||
namespace = ns[0]
|
segments := []string{}
|
||||||
|
for i := range ns {
|
||||||
|
if ns[i] != "" {
|
||||||
|
segments = append(segments, ns[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
namespace = strings.Join(segments, ".")
|
||||||
}
|
}
|
||||||
return namespace
|
return namespace
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
db_test.go
10
db_test.go
|
|
@ -58,6 +58,12 @@ func TestImplementations(t *testing.T) {
|
||||||
cases = append(cases, cacheFile)
|
cases = append(cases, cacheFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if redis, err := NewRedis("localhost:6379", "", ""); err != nil {
|
||||||
|
t.Errorf("cannot make redis: %v", err)
|
||||||
|
} else {
|
||||||
|
cases = append(cases, redis)
|
||||||
|
}
|
||||||
|
|
||||||
if bolt, err := NewBolt(path.Join(dir, "bolt")); err != nil {
|
if bolt, err := NewBolt(path.Join(dir, "bolt")); err != nil {
|
||||||
t.Errorf("cannot make bolt: %v", err)
|
t.Errorf("cannot make bolt: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -155,6 +161,10 @@ func TestToFromString(t *testing.T) {
|
||||||
key: "map",
|
key: "map",
|
||||||
t: MAP,
|
t: MAP,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: "redis",
|
||||||
|
t: REDIS,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: "minio",
|
key: "minio",
|
||||||
t: MINIO,
|
t: MINIO,
|
||||||
|
|
|
||||||
20
mongo.go
20
mongo.go
|
|
@ -5,12 +5,12 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mongodb/mongo-go-driver/mongo"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"github.com/mongodb/mongo-go-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"github.com/mongodb/mongo-go-driver/mongo/readconcern"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"github.com/mongodb/mongo-go-driver/mongo/readpref"
|
"go.mongodb.org/mongo-driver/mongo/readconcern"
|
||||||
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||||
"gopkg.in/mgo.v2/bson"
|
"go.mongodb.org/mongo-driver/mongo/writeconcern"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mongo struct {
|
type Mongo struct {
|
||||||
|
|
@ -26,8 +26,8 @@ func NewMongo(addr string, auth ...string) (*Mongo, error) {
|
||||||
Password: auth[1],
|
Password: auth[1],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
db, err := mongo.NewClientWithOptions(
|
db, err := mongo.NewClient(
|
||||||
"mongodb://"+addr,
|
options.Client().ApplyURI("mongodb://"+addr),
|
||||||
options.Client().SetReadConcern(readconcern.Local()),
|
options.Client().SetReadConcern(readconcern.Local()),
|
||||||
options.Client().SetReadPreference(readpref.PrimaryPreferred()),
|
options.Client().SetReadPreference(readpref.PrimaryPreferred()),
|
||||||
options.Client().SetWriteConcern(writeconcern.New(
|
options.Client().SetWriteConcern(writeconcern.New(
|
||||||
|
|
@ -68,8 +68,8 @@ func (mg *Mongo) Get(key string, ns ...string) ([]byte, error) {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
elem, err := cursor.DecodeBytes()
|
var elem bson.Raw
|
||||||
if err != nil {
|
if err := cursor.Decode(&elem); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
raw, err := elem.LookupErr("value")
|
raw, err := elem.LookupErr("value")
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gomodule/redigo/redis"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Redis struct {
|
||||||
|
client redis.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRedis(addr, user, pass string) (*Redis, error) {
|
||||||
|
opts := []redis.DialOption{}
|
||||||
|
if pass != "" {
|
||||||
|
opts = append(opts, redis.DialPassword(pass))
|
||||||
|
}
|
||||||
|
opts = append(opts, redis.DialConnectTimeout(time.Second*10))
|
||||||
|
client, err := redis.Dial("tcp", addr, opts...)
|
||||||
|
return &Redis{
|
||||||
|
client: client,
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Redis) String() string {
|
||||||
|
return fmt.Sprintf("%v", *m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Redis) Close() error {
|
||||||
|
return m.client.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Redis) Get(key string, ns ...string) ([]byte, error) {
|
||||||
|
key = resolveNamespace(append(ns, key))
|
||||||
|
log.Println(key)
|
||||||
|
resp, err := m.client.Do("GET", key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if resp == nil {
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
|
b, ok := resp.([]byte)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("resp not a []byte")
|
||||||
|
}
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Redis) Set(key string, value []byte, ns ...string) error {
|
||||||
|
namespace := resolveNamespace(append(ns, key))
|
||||||
|
_, err := m.client.Do("SET", namespace, value)
|
||||||
|
return err
|
||||||
|
}
|
||||||
6
type.go
6
type.go
|
|
@ -8,7 +8,9 @@ type Type int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MAP = Type(iota)
|
MAP = Type(iota)
|
||||||
|
REDIS = Type(iota)
|
||||||
BOLT = Type(iota)
|
BOLT = Type(iota)
|
||||||
|
COCKROACH = Type(iota)
|
||||||
CACHE = Type(iota)
|
CACHE = Type(iota)
|
||||||
LEVELDB = Type(iota)
|
LEVELDB = Type(iota)
|
||||||
MEMCACHE = Type(iota)
|
MEMCACHE = Type(iota)
|
||||||
|
|
@ -19,8 +21,12 @@ const (
|
||||||
|
|
||||||
func (t Type) String() string {
|
func (t Type) String() string {
|
||||||
switch t {
|
switch t {
|
||||||
|
case REDIS:
|
||||||
|
return "redis"
|
||||||
case MAP:
|
case MAP:
|
||||||
return "map"
|
return "map"
|
||||||
|
case COCKROACH:
|
||||||
|
return "cockroach"
|
||||||
case BOLT:
|
case BOLT:
|
||||||
return "bolt"
|
return "bolt"
|
||||||
case MINIO:
|
case MINIO:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue