Skip to content

Commit

Permalink
Isolate driver kind
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Mar 18, 2022
1 parent d1f96ad commit e85126b
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The cache you can choose between drivers for different storage.
Very simple.

```golang
cache, err := cache.New(drivers.BUNTDB, opts...)
cache, err := cache.New(kind.BUNTDB, opts...)
if err != nil {
fmt.Println("Error:", err)
return
Expand Down
4 changes: 2 additions & 2 deletions examples/buntdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/thiagozs/go-cache/v1/cache"
"github.com/thiagozs/go-cache/v1/cache/drivers"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
)

Expand All @@ -19,7 +19,7 @@ func main() {
options.OptLogDisable(false),
}

cache, err := cache.New(drivers.BUNTDB, opts...)
cache, err := cache.New(kind.BUNTDB, opts...)
if err != nil {
fmt.Println("Error:", err)
return
Expand Down
4 changes: 2 additions & 2 deletions examples/gocache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/thiagozs/go-cache/v1/cache"
"github.com/thiagozs/go-cache/v1/cache/drivers"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
)

Expand All @@ -18,7 +18,7 @@ func main() {
options.OptTimeCleanUpInt(time.Second * 120),
}

cache, err := cache.New(drivers.GOCACHE, opts...)
cache, err := cache.New(kind.GOCACHE, opts...)
if err != nil {
fmt.Println("Error:", err)
return
Expand Down
4 changes: 2 additions & 2 deletions examples/redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/thiagozs/go-cache/v1/cache"
"github.com/thiagozs/go-cache/v1/cache/drivers"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
)

Expand All @@ -17,7 +17,7 @@ func main() {
options.OptPort(6379),
}

cache, err := cache.New(drivers.REDIS, opts...)
cache, err := cache.New(kind.REDIS, opts...)
if err != nil {
fmt.Println("Error:", err)
return
Expand Down
13 changes: 8 additions & 5 deletions v1/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cache

import (
"github.com/rs/zerolog"
"github.com/thiagozs/go-cache/v1/cache/drivers"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
)

Expand All @@ -13,14 +13,13 @@ type CachePort interface {
WriteKeyValAsJSON(key string, val interface{}) error
WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error
GetVal(key string) (string, error)
GetDriver() kind.Driver
}
type cache struct {
db CachePort
ttl int
log zerolog.Logger
db CachePort
}

func New(driver drivers.Driver, opts ...options.Options) (CachePort, error) {
func New(driver kind.Driver, opts ...options.Options) (CachePort, error) {

port, err := drivers.NewDriver(driver, opts...)
if err != nil {
Expand Down Expand Up @@ -55,3 +54,7 @@ func (c *cache) WriteKeyValAsJSON(key string, val interface{}) error {
func (c *cache) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
return c.db.WriteKeyValAsJSONTTL(key, val, ttlSeconds)
}

func (c *cache) GetDriver() kind.Driver {
return c.db.GetDriver()
}
14 changes: 11 additions & 3 deletions v1/cache/drivers/buntdb/buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/rs/zerolog"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
"github.com/thiagozs/go-utils/files"
"github.com/tidwall/buntdb"
Expand All @@ -20,6 +21,7 @@ type BuntDBLayerRepo interface {
WriteKeyValAsJSON(key string, val interface{}) error
WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error
GetVal(key string) (string, error)
GetDriver() kind.Driver
}

type buntdblayer struct {
Expand All @@ -28,20 +30,21 @@ type buntdblayer struct {
folder string
ttl int
log zerolog.Logger
driver kind.Driver
}

func NewBuntDB(opts ...options.Options) (BuntDBLayerRepo, error) {
func NewBuntDB(driver kind.Driver, opts ...options.Options) (BuntDBLayerRepo, error) {
mts := &options.OptionsCfg{}
for _, op := range opts {
err := op(mts)
if err != nil {
return nil, err
}
}
return newInstance(mts)
return newInstance(driver, mts)
}

func newInstance(opt *options.OptionsCfg) (BuntDBLayerRepo, error) {
func newInstance(driver kind.Driver, opt *options.OptionsCfg) (BuntDBLayerRepo, error) {

zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
Expand Down Expand Up @@ -77,6 +80,7 @@ func newInstance(opt *options.OptionsCfg) (BuntDBLayerRepo, error) {
file: opt.GetFileName(),
ttl: opt.GetTTL(),
log: log,
driver: driver,
}, nil
}

Expand Down Expand Up @@ -177,3 +181,7 @@ func (d *buntdblayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSecon
}
return d.WriteKeyValTTL(key, string(valueAsJSON), ttlSeconds)
}

func (d *buntdblayer) GetDriver() kind.Driver {
return d.driver
}
32 changes: 13 additions & 19 deletions v1/cache/drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@ package drivers
import (
buntdblayer "github.com/thiagozs/go-cache/v1/cache/drivers/buntdb"
gocachelayer "github.com/thiagozs/go-cache/v1/cache/drivers/gocache"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
redislayer "github.com/thiagozs/go-cache/v1/cache/drivers/redis"
"github.com/thiagozs/go-cache/v1/cache/options"
)

type Driver int

const (
BUNTDB Driver = iota
REDIS
GOCACHE
)

func (d Driver) String() string {
return []string{"buntdb", "redis", "gocache"}[d]
}

type Drivers struct {
db DriverPort
}
Expand All @@ -30,18 +19,19 @@ type DriverPort interface {
WriteKeyValAsJSON(key string, val interface{}) error
WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error
GetVal(key string) (string, error)
GetDriver() kind.Driver
}

func NewDriver(driver Driver, opts ...options.Options) (DriverPort, error) {
func NewDriver(driver kind.Driver, opts ...options.Options) (DriverPort, error) {
var db DriverPort
var err error
switch driver {
case BUNTDB:
db, err = buntdblayer.NewBuntDB(opts...)
case REDIS:
db, err = redislayer.NewRedis(opts...)
case GOCACHE:
db, err = gocachelayer.NewMemory(opts...)
case kind.BUNTDB:
db, err = buntdblayer.NewBuntDB(driver, opts...)
case kind.REDIS:
db, err = redislayer.NewRedis(driver, opts...)
case kind.GOCACHE:
db, err = gocachelayer.NewMemory(driver, opts...)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -72,3 +62,7 @@ func (d *Drivers) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds i
func (d *Drivers) GetVal(key string) (string, error) {
return d.db.GetVal(key)
}

func (d *Drivers) GetDriver() kind.Driver {
return d.db.GetDriver()
}
14 changes: 11 additions & 3 deletions v1/cache/drivers/gocache/gocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/patrickmn/go-cache"
"github.com/rs/zerolog"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
)

Expand All @@ -19,27 +20,29 @@ type MemoryLayerRepo interface {
WriteKeyValAsJSON(key string, val interface{}) error
WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error
GetVal(key string) (string, error)
GetDriver() kind.Driver
}

type memorylayer struct {
tExpiration time.Duration
tCleanupInt time.Duration
log zerolog.Logger
cache *cache.Cache
driver kind.Driver
}

func NewMemory(opts ...options.Options) (MemoryLayerRepo, error) {
func NewMemory(driver kind.Driver, opts ...options.Options) (MemoryLayerRepo, error) {
mts := &options.OptionsCfg{}
for _, op := range opts {
err := op(mts)
if err != nil {
return nil, err
}
}
return newInstance(mts)
return newInstance(driver, mts)
}

func newInstance(opt *options.OptionsCfg) (MemoryLayerRepo, error) {
func newInstance(driver kind.Driver, opt *options.OptionsCfg) (MemoryLayerRepo, error) {

zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
Expand All @@ -61,6 +64,7 @@ func newInstance(opt *options.OptionsCfg) (MemoryLayerRepo, error) {
cache: c,
tExpiration: opt.GetTExpiration(),
tCleanupInt: opt.GetTCleanUpInt(),
driver: driver,
}, nil
}

Expand Down Expand Up @@ -134,3 +138,7 @@ func (m *memorylayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSecon

return m.WriteKeyValTTL(key, string(valueAsJSON), ttlSeconds)
}

func (d *memorylayer) GetDriver() kind.Driver {
return d.driver
}
13 changes: 13 additions & 0 deletions v1/cache/drivers/kind/kind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kind

type Driver int

const (
BUNTDB Driver = iota
REDIS
GOCACHE
)

func (d Driver) String() string {
return []string{"buntdb", "redis", "gocache"}[d]
}
14 changes: 11 additions & 3 deletions v1/cache/drivers/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/go-redis/redis/v7"
"github.com/rs/zerolog"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
)

Expand All @@ -18,6 +19,7 @@ type RedisLayerRepo interface {
WriteKeyValAsJSON(key string, val interface{}) error
WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error
GetVal(key string) (string, error)
GetDriver() kind.Driver
}

type redisblayer struct {
Expand All @@ -28,20 +30,21 @@ type redisblayer struct {
ttl int
log zerolog.Logger
rdb *redis.Client
driver kind.Driver
}

func NewRedis(opts ...options.Options) (RedisLayerRepo, error) {
func NewRedis(driver kind.Driver, opts ...options.Options) (RedisLayerRepo, error) {
mts := &options.OptionsCfg{}
for _, op := range opts {
err := op(mts)
if err != nil {
return nil, err
}
}
return newInstance(mts)
return newInstance(driver, mts)
}

func newInstance(opt *options.OptionsCfg) (RedisLayerRepo, error) {
func newInstance(driver kind.Driver, opt *options.OptionsCfg) (RedisLayerRepo, error) {

zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
Expand Down Expand Up @@ -76,6 +79,7 @@ func newInstance(opt *options.OptionsCfg) (RedisLayerRepo, error) {
port: opt.GetPort(),
ttl: opt.GetTTL(),
rdb: rdb,
driver: driver,
}, nil
}

Expand Down Expand Up @@ -140,3 +144,7 @@ func (d *redisblayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSecon

return d.WriteKeyValTTL(key, string(valueAsJSON), ttlSeconds)
}

func (d *redisblayer) GetDriver() kind.Driver {
return d.driver
}

0 comments on commit e85126b

Please sign in to comment.