Skip to content

Commit

Permalink
add incr and decr
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Dec 1, 2023
1 parent 4e02d24 commit 99ec70e
Show file tree
Hide file tree
Showing 20 changed files with 557 additions and 26 deletions.
71 changes: 71 additions & 0 deletions buntdb/buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"github.com/rs/zerolog"
kind "github.com/thiagozs/go-cache/kind"
"github.com/thiagozs/go-utils/files"
"github.com/thiagozs/go-xutils"
"github.com/tidwall/buntdb"
)

type BuntDBLayer struct {
params *BuntDBParams
utils *xutils.XUtils
}

func NewBuntDB(opts ...Options) (*BuntDBLayer, error) {
Expand Down Expand Up @@ -57,6 +59,7 @@ func NewBuntDB(opts ...Options) (*BuntDBLayer, error) {

return &BuntDBLayer{
params: params,
utils: xutils.New(),
}, nil
}

Expand Down Expand Up @@ -160,3 +163,71 @@ func (d *BuntDBLayer) WriteKeyValAsJSONTTL(key string, val any, insec int) error
func (d *BuntDBLayer) GetDriver() kind.Driver {
return d.params.GetDriver()
}

func (d *BuntDBLayer) Incr(key string) (int64, error) {
val, err := d.GetVal(key)
if err != nil {
d.params.log.Debug().Err(err).Msg("Incr")
return 0, err
}

d.params.log.Debug().Str("method", "incr").
Str("key", key).
Str("value", val).
Msg("Incr")

value, err := d.utils.Convs().ToInt64(val)
if err != nil {
d.params.log.Debug().Err(err).Msg("Incr")
return 0, err
}

value = value + 1

err = d.WriteKeyVal(key, fmt.Sprintf("%d", value))
if err != nil {
d.params.log.Debug().Err(err).Msg("Incr")
return 0, err
}

d.params.log.Debug().Str("method", "incr").
Str("key", key).
Int64("value", value).
Msg("Incr")

return value, nil
}

func (d *BuntDBLayer) Decr(key string) (int64, error) {
val, err := d.GetVal(key)
if err != nil {
d.params.log.Debug().Err(err).Msg("Decr")
return 0, err
}

d.params.log.Debug().Str("method", "decr").
Str("key", key).
Str("value", val).
Msg("Decr")

value, err := d.utils.Convs().ToInt64(val)
if err != nil {
d.params.log.Debug().Err(err).Msg("Decr")
return 0, err
}

value = value - 1

err = d.WriteKeyVal(key, fmt.Sprintf("%d", value))
if err != nil {
d.params.log.Debug().Err(err).Msg("Decr")
return 0, err
}

d.params.log.Debug().Str("method", "decr").
Str("key", key).
Int64("value", value).
Msg("Decr")

return value, nil
}
32 changes: 31 additions & 1 deletion buntdb/buntdb_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions buntdb/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ func (d *Cache) GetVal(key string) (string, error) {
func (d *Cache) GetDriver() kind.Driver {
return d.db.GetDriver()
}

func (d *Cache) Incr(key string) (int64, error) {
return d.db.Incr(key)
}

func (d *Cache) Decr(key string) (int64, error) {
return d.db.Decr(key)
}
32 changes: 31 additions & 1 deletion cache_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 103 additions & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/thiagozs/go-cache/kind"
"github.com/thiagozs/go-cache/redis"

"go.uber.org/mock/gomock"
"github.com/golang/mock/gomock"
)

func TestNewDriver(t *testing.T) {
Expand Down Expand Up @@ -427,3 +427,105 @@ func TestCacheWriteKeyValAsJSONTTL(t *testing.T) {
})
}
}

func TestCacheIncr(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

tests := []struct {
name string
mock any
key string
expected int64
}{
{
name: "Redis",
mock: redis.NewMockRedisLayerRepo(ctrl),
key: "key1",
expected: 1,
},
{
name: "GoCache",
mock: gocache.NewMockGocacheLayerRepo(ctrl),
key: "key2",
expected: 2,
},
{
name: "BuntDB",
mock: buntdb.NewMockBuntDBLayerRepo(ctrl),
key: "key3",
expected: 3,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
switch v := tt.mock.(type) {
case *redis.MockRedisLayerRepo:
v.EXPECT().Incr(tt.key).Return(tt.expected, nil).Times(1)
case *gocache.MockGocacheLayerRepo:
v.EXPECT().Incr(tt.key).Return(tt.expected, nil).Times(1)
case *buntdb.MockBuntDBLayerRepo:
v.EXPECT().Incr(tt.key).Return(tt.expected, nil).Times(1)
}

cache, err := New(OptCache(tt.mock.(CacheRepo)))
assert.NoError(t, err)

value, err := cache.Incr(tt.key)
assert.NoError(t, err)
assert.Equal(t, tt.expected, value)
})
}
}

func TestCacheDecr(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

tests := []struct {
name string
mock any
key string
expected int64
}{
{
name: "Redis",
mock: redis.NewMockRedisLayerRepo(ctrl),
key: "key1",
expected: 1,
},
{
name: "GoCache",
mock: gocache.NewMockGocacheLayerRepo(ctrl),
key: "key2",
expected: 2,
},
{
name: "BuntDB",
mock: buntdb.NewMockBuntDBLayerRepo(ctrl),
key: "key3",
expected: 3,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
switch v := tt.mock.(type) {
case *redis.MockRedisLayerRepo:
v.EXPECT().Decr(tt.key).Return(tt.expected, nil).Times(1)
case *gocache.MockGocacheLayerRepo:
v.EXPECT().Decr(tt.key).Return(tt.expected, nil).Times(1)
case *buntdb.MockBuntDBLayerRepo:
v.EXPECT().Decr(tt.key).Return(tt.expected, nil).Times(1)
}

cache, err := New(OptCache(tt.mock.(CacheRepo)))
assert.NoError(t, err)

value, err := cache.Decr(tt.key)
assert.NoError(t, err)
assert.Equal(t, tt.expected, value)
})
}
}
16 changes: 16 additions & 0 deletions examples/buntdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@ func main() {
fmt.Println("v1:", v1)
fmt.Println("v2:", v2)

cache.WriteKeyVal("key5", "1")

v5, err := cache.Incr("key5")
if err != nil {
fmt.Println("Error:", err)
}

fmt.Println("v5:", v5)

v6, err := cache.Decr("key5")
if err != nil {
fmt.Println("Error:", err)
}

fmt.Println("v6:", v6)

}
35 changes: 35 additions & 0 deletions examples/buntdb/tmp/cache/cache.db
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,38 @@ $4
key2
$6
value2
*3
$3
set
$4
key1
$6
value1
*3
$3
set
$4
key2
$6
value2
*3
$3
set
$4
key5
$1
1
*3
$3
set
$4
key5
$1
2
*3
$3
set
$4
key5
$1
1
Loading

0 comments on commit 99ec70e

Please sign in to comment.