Skip to content

Commit

Permalink
Update redis driver
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Sep 27, 2023
1 parent e85126b commit fe5517c
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 104 deletions.
2 changes: 0 additions & 2 deletions examples/buntdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
)

func main() {
fmt.Println("Hello World!")

opts := []options.Options{
options.OptFolder("./tmp/cache"),
options.OptFileName("cache.db"),
Expand Down
6 changes: 6 additions & 0 deletions examples/redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ func main() {
fmt.Println("v1:", v1)
fmt.Println("v2:", v2)

v3, err := cache.GetVal("key3")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("v3:", v3)
}
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
module github.com/thiagozs/go-cache

go 1.17
go 1.21

require (
github.com/rs/zerolog v1.26.0
github.com/thiagozs/go-utils v0.0.0-20211118150243-5cfe9a632a4b
)

require (
github.com/go-redis/redis/v7 v7.4.1
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/redis/go-redis/v9 v9.2.1 // indirect
)

require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/tidwall/btree v0.6.1 // indirect
github.com/tidwall/buntdb v1.2.7
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-redis/redis/v7 v7.4.1 h1:PASvf36gyUpr2zdOUS/9Zqc80GbM+9BDyiJSJDDOrTI=
github.com/go-redis/redis/v7 v7.4.1/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
Expand All @@ -18,6 +22,8 @@ github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE=
github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo=
Expand Down
36 changes: 13 additions & 23 deletions v1/cache/drivers/buntdb/buntdb.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package buntdblayer
package buntdb

import (
"encoding/json"
Expand All @@ -14,17 +14,7 @@ import (
"github.com/tidwall/buntdb"
)

type BuntDBLayerRepo interface {
WriteKeyVal(key string, val string) error
WriteKeyValTTL(key string, val string, ttlSeconds int) error
DeleteKey(key string) (string, error)
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 {
type BuntDBLayer struct {
db *buntdb.DB
file string
folder string
Expand All @@ -33,18 +23,18 @@ type buntdblayer struct {
driver kind.Driver
}

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

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

zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
Expand Down Expand Up @@ -74,7 +64,7 @@ func newInstance(driver kind.Driver, opt *options.OptionsCfg) (BuntDBLayerRepo,
log.Info().Err(err).Msg("could not open data file path")
return nil, err
}
return &buntdblayer{
return &BuntDBLayer{
db: db,
folder: opt.GetFolder(),
file: opt.GetFileName(),
Expand All @@ -84,7 +74,7 @@ func newInstance(driver kind.Driver, opt *options.OptionsCfg) (BuntDBLayerRepo,
}, nil
}

func (d *buntdblayer) GetVal(key string) (string, error) {
func (d *BuntDBLayer) GetVal(key string) (string, error) {
var value string
err := d.db.View(func(tx *buntdb.Tx) error {
val, err := tx.Get(key)
Expand All @@ -102,7 +92,7 @@ func (d *buntdblayer) GetVal(key string) (string, error) {
return value, err
}

func (d *buntdblayer) DeleteKey(key string) (string, error) {
func (d *BuntDBLayer) DeleteKey(key string) (string, error) {
var value string
err := d.db.Update(func(tx *buntdb.Tx) error {
val, err := tx.Delete(key)
Expand All @@ -120,7 +110,7 @@ func (d *buntdblayer) DeleteKey(key string) (string, error) {
return value, err
}

func (d *buntdblayer) WriteKeyVal(key string, val string) error {
func (d *BuntDBLayer) WriteKeyVal(key string, val string) error {
err := d.db.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set(key, val, nil)
d.log.Debug().Err(err).Msg("WriteKeyVal")
Expand All @@ -136,7 +126,7 @@ func (d *buntdblayer) WriteKeyVal(key string, val string) error {
return nil
}

func (d *buntdblayer) WriteKeyValTTL(key string, val string, ttlSeconds int) error {
func (d *BuntDBLayer) WriteKeyValTTL(key string, val string, ttlSeconds int) error {
if ttlSeconds == 0 {
d.log.Debug().Int("ttl_seconds", d.ttl).Msg("WriteKeyValTTL")
ttlSeconds = d.ttl
Expand All @@ -159,7 +149,7 @@ func (d *buntdblayer) WriteKeyValTTL(key string, val string, ttlSeconds int) err
return nil
}

func (d *buntdblayer) WriteKeyValAsJSON(key string, val interface{}) error {
func (d *BuntDBLayer) WriteKeyValAsJSON(key string, val interface{}) error {
valueAsJSON, err := json.Marshal(val)
if err != nil {
d.log.Debug().Str("method", "write").Err(err).Msg("WriteKeyValAsJSON")
Expand All @@ -168,7 +158,7 @@ func (d *buntdblayer) WriteKeyValAsJSON(key string, val interface{}) error {
return d.WriteKeyVal(key, string(valueAsJSON))
}

func (d *buntdblayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
func (d *BuntDBLayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
if ttlSeconds == 0 {
d.log.Debug().Int("ttl_seconds", d.ttl).Msg("WriteKeyValTTL")
ttlSeconds = d.ttl
Expand All @@ -182,6 +172,6 @@ func (d *buntdblayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSecon
return d.WriteKeyValTTL(key, string(valueAsJSON), ttlSeconds)
}

func (d *buntdblayer) GetDriver() kind.Driver {
func (d *BuntDBLayer) GetDriver() kind.Driver {
return d.driver
}
31 changes: 12 additions & 19 deletions v1/cache/drivers/drivers.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
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/buntdb"
"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/drivers/redis"
"github.com/thiagozs/go-cache/v1/cache/options"
)

type Drivers struct {
db DriverPort
db DriverRepo
}

type DriverPort interface {
WriteKeyVal(key string, val string) error
WriteKeyValTTL(key string, val string, ttlSeconds int) error
DeleteKey(key string) (string, error)
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 kind.Driver, opts ...options.Options) (DriverPort, error) {
var db DriverPort
func NewDriver(driver kind.Driver, opts ...options.Options) (*Drivers, error) {
var db DriverRepo
var err error

switch driver {
case kind.BUNTDB:
db, err = buntdblayer.NewBuntDB(driver, opts...)
db, err = buntdb.NewBuntDB(driver, opts...)
case kind.REDIS:
db, err = redislayer.NewRedis(driver, opts...)
db, err = redis.NewRedis(driver, opts...)
case kind.GOCACHE:
db, err = gocachelayer.NewMemory(driver, opts...)
db, err = gocache.NewMemory(driver, opts...)
}

if err != nil {
return nil, err
}

return &Drivers{db: db}, nil
}

Expand Down
36 changes: 13 additions & 23 deletions v1/cache/drivers/gocache/gocache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package memorylayer
package gocache

import (
"encoding/json"
Expand All @@ -13,36 +13,26 @@ import (
"github.com/thiagozs/go-cache/v1/cache/options"
)

type MemoryLayerRepo interface {
WriteKeyVal(key string, val string) error
WriteKeyValTTL(key string, val string, ttlSeconds int) error
DeleteKey(key string) (string, error)
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 {
type MemoryLayer struct {
tExpiration time.Duration
tCleanupInt time.Duration
log zerolog.Logger
cache *cache.Cache
driver kind.Driver
}

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

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

zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
Expand All @@ -59,7 +49,7 @@ func newInstance(driver kind.Driver, opt *options.OptionsCfg) (MemoryLayerRepo,

c := cache.New(opt.GetTExpiration(), opt.GetTCleanUpInt())

return &memorylayer{
return &MemoryLayer{
log: log,
cache: c,
tExpiration: opt.GetTExpiration(),
Expand All @@ -68,7 +58,7 @@ func newInstance(driver kind.Driver, opt *options.OptionsCfg) (MemoryLayerRepo,
}, nil
}

func (m *memorylayer) GetVal(key string) (string, error) {
func (m *MemoryLayer) GetVal(key string) (string, error) {
iface, found := m.cache.Get(key)
if !found {
err := fmt.Errorf("not found")
Expand All @@ -83,15 +73,15 @@ func (m *memorylayer) GetVal(key string) (string, error) {
return str, nil
}

func (m *memorylayer) DeleteKey(key string) (string, error) {
func (m *MemoryLayer) DeleteKey(key string) (string, error) {
m.log.Debug().Str("method", "delete").
Str("key", key).
Msg("DeleteKey")
m.cache.Delete(key)
return "", nil
}

func (m *memorylayer) WriteKeyVal(key string, val string) error {
func (m *MemoryLayer) WriteKeyVal(key string, val string) error {
m.log.Debug().Str("method", "write").
Str("key", key).
Str("value", val).
Expand All @@ -100,7 +90,7 @@ func (m *memorylayer) WriteKeyVal(key string, val string) error {
return nil
}

func (m *memorylayer) WriteKeyValTTL(key string, val string, ttlSeconds int) error {
func (m *MemoryLayer) WriteKeyValTTL(key string, val string, ttlSeconds int) error {
ttlc := time.Duration(60) * time.Second
if ttlSeconds > 0 {
m.log.Debug().Int("ttl_seconds", int(ttlc)).Msg("WriteKeyValTTL")
Expand All @@ -115,7 +105,7 @@ func (m *memorylayer) WriteKeyValTTL(key string, val string, ttlSeconds int) err
return nil
}

func (m *memorylayer) WriteKeyValAsJSON(key string, val interface{}) error {
func (m *MemoryLayer) WriteKeyValAsJSON(key string, val interface{}) error {
valueAsJSON, err := json.Marshal(val)
if err != nil {
m.log.Debug().Str("method", "json.Marshal").Err(err).Msg("WriteKeyValAsJSON")
Expand All @@ -124,7 +114,7 @@ func (m *memorylayer) WriteKeyValAsJSON(key string, val interface{}) error {
return m.WriteKeyVal(key, string(valueAsJSON))
}

func (m *memorylayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
func (m *MemoryLayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
ttlc := 60
if ttlSeconds > 0 {
m.log.Debug().Int("ttl_seconds", ttlc).Msg("WriteKeyValAsJSONTTL")
Expand All @@ -139,6 +129,6 @@ func (m *memorylayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSecon
return m.WriteKeyValTTL(key, string(valueAsJSON), ttlSeconds)
}

func (d *memorylayer) GetDriver() kind.Driver {
func (d *MemoryLayer) GetDriver() kind.Driver {
return d.driver
}
Loading

0 comments on commit fe5517c

Please sign in to comment.