Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: implement decrement and upgrade go1.22 #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kumparan/cacher

go 1.18
go 1.22

require (
github.com/alicebob/miniredis/v2 v2.30.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/CloudyKit/jet/v3 v3.0.0/go.mod h1:HKQPgSJmdK8hdoAbKUUWajkHyHo4RaU5rMd
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw=
github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
Expand Down
16 changes: 16 additions & 0 deletions keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type (
Purge(string) error
DeleteByKeys([]string) error
IncreaseCachedValueByOne(key string) error
DecreaseCachedValueByOne(key string) error

AcquireLock(string) (*redsync.Mutex, error)
SetDefaultTTL(time.Duration)
Expand Down Expand Up @@ -663,6 +664,21 @@ func (k *keeper) IncreaseCachedValueByOne(key string) error {
return err
}

// DecreaseCachedValueByOne will decrement the number stored at key by one.
func (k *keeper) DecreaseCachedValueByOne(key string) error {
if k.disableCaching {
return nil
}

client := k.connPool.Get()
defer func() {
_ = client.Close()
}()

_, err := client.Do("DECR", key)
return err
}

// AcquireLock :nodoc:
func (k *keeper) AcquireLock(key string) (*redsync.Mutex, error) {
p := redigosync.NewPool(k.lockConnPool)
Expand Down
31 changes: 31 additions & 0 deletions keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,37 @@ func TestIncreaseCachedValueByOne(t *testing.T) {
assert.EqualValues(t, 1, count)
}

func TestDecreaseCachedValueByOne(t *testing.T) {
// Initialize new cache keeper
k := NewKeeper()

m, err := miniredis.Run()
assert.NoError(t, err)

r := newRedisConn(m.Addr())
k.SetConnectionPool(r)
k.SetLockConnectionPool(r)

testKey := "decrease-test"
_, mu, err := k.GetOrLock(testKey)
assert.NoError(t, err)

err = k.Store(mu, NewItem(testKey, 1))
assert.NoError(t, err)

err = k.DecreaseCachedValueByOne(testKey)
assert.NoError(t, err)

reply, _, err := k.GetOrLock(testKey)
assert.NoError(t, err)

var count int64
bt, _ := reply.([]byte)
err = json.Unmarshal(bt, &count)
assert.NoError(t, err)
assert.EqualValues(t, 0, count)
}

func TestDeleteByKeys(t *testing.T) {
t.Run("success", func(t *testing.T) {
// Initialize new cache keeper
Expand Down
Loading