Skip to content

Commit

Permalink
Fix set the data with no expiration is ineffective in the buntdb (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev committed Jun 9, 2023
1 parent c774044 commit ddf2345
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.0
v0.1.1
1 change: 1 addition & 0 deletions boltdb/boltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
)

func TestBoltDBCache(t *testing.T) {
testutil.TestCache(t, connectionString, testutil.NoExpiration)
testutil.TestCache(t, connectionString, expiration)
testutil.TestCache(t, connectionStringWithBucket, expiration)
}
Expand Down
6 changes: 5 additions & 1 deletion buntdb/buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ func (c *buntDBCache) Set(k string, v any, expiration time.Duration) error {
if err != nil {
return err
}
expires := true
if expiration <= 0 {
expires = false
}
return c.db.Update(func(tx *buntdb.Tx) error {
_, _, setErr := tx.Set(k, string(data), &buntdb.SetOptions{Expires: true, TTL: expiration})
_, _, setErr := tx.Set(k, string(data), &buntdb.SetOptions{Expires: expires, TTL: expiration})
return setErr
})
}
Expand Down
12 changes: 12 additions & 0 deletions buntdb/buntdb_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ func BenchmarkBuntDBCache_Set(b *testing.B) {
func BenchmarkBuntDBCache_Remove(b *testing.B) {
testutil.BenchmarkCacheRemove(b, connectionString, expiration)
}

func BenchmarkBuntDBCache_Memory_Get(b *testing.B) {
testutil.BenchmarkCacheGet(b, memoryConnectionString, expiration)
}

func BenchmarkBuntDBCache_Memory_Set(b *testing.B) {
testutil.BenchmarkCacheSet(b, memoryConnectionString, expiration)
}

func BenchmarkBuntDBCache_Memory_Remove(b *testing.B) {
testutil.BenchmarkCacheRemove(b, memoryConnectionString, expiration)
}
8 changes: 6 additions & 2 deletions buntdb/buntdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
)

var (
connectionString = testutil.BuntDBConnectionString
expiration = testutil.DefaultExpiration
connectionString = testutil.BuntDBConnectionString
memoryConnectionString = testutil.BuntDBMemoryConnectionString
expiration = testutil.DefaultExpiration
)

func TestBuntDBCache(t *testing.T) {
testutil.TestCache(t, connectionString, testutil.NoExpiration)
testutil.TestCache(t, memoryConnectionString, testutil.NoExpiration)
testutil.TestCache(t, connectionString, expiration)
testutil.TestCache(t, memoryConnectionString, expiration)
}

func TestBuntDBCache_NewCache_WithNilURL(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import (
)

func TestCache(t *testing.T) {
testutil.TestCache(t, testutil.MemoryConnectionString, testutil.NoExpiration)
testutil.TestCache(t, testutil.MemoryConnectionString, testutil.DefaultExpiration)
}
1 change: 1 addition & 0 deletions etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
)

func TestEtcdCache(t *testing.T) {
testutil.TestCache(t, connectionString, testutil.NoExpiration)
testutil.TestCache(t, connectionString, expiration)
}

Expand Down
1 change: 1 addition & 0 deletions extension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestExtension(t *testing.T) {
}{
{testutil.MemoryConnectionString},
{testutil.BuntDBConnectionString},
{testutil.BuntDBMemoryConnectionString},
{testutil.EtcdConnectionString},
{testutil.RedisConnectionString},
}
Expand Down
22 changes: 15 additions & 7 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const (
// MemoryConnectionString a memory cache driver test connection string
MemoryConnectionString = "memory:"
// BuntDBConnectionString a buntdb cache driver test connection string
BuntDBConnectionString = "buntdb:https://:memory:"
BuntDBConnectionString = "buntdb:https://buntdb.db"
// BuntDBMemoryConnectionString a buntdb memory cache driver test connection string
BuntDBMemoryConnectionString = "buntdb:https://:memory:"
// EtcdConnectionString a etcd cache driver test connection string
EtcdConnectionString = "etcd:https://127.0.0.1:2379?dial_timeout=5s"
// RedisConnectionString a redis cache driver test connection string
Expand All @@ -21,6 +23,8 @@ const (
BoltDBConnectionString = "boltdb:https://boltdb.db"
// DefaultExpiration the default expiration time for cache driver tests
DefaultExpiration = time.Second * 3
// NoExpiration means never expire
NoExpiration = 0
)

// TestCache test the cache with the passed connection string
Expand Down Expand Up @@ -67,6 +71,8 @@ func TestCache(t *testing.T, conn string, expiration time.Duration) {
return
}

time.Sleep(time.Millisecond)

// get data after set
err = c.Get(tc.k, &actual)
if err != nil {
Expand All @@ -77,12 +83,14 @@ func TestCache(t *testing.T, conn string, expiration time.Duration) {
return
}

// get data after data is expired
<-time.After(expiration + time.Second*2)
err = c.Get(tc.k, &actual)
if !errors.Is(err, nscache.ErrNil) {
t.Errorf("Get: expect to get error => %v, but get %v, k=%v", nscache.ErrNil, err, tc.k)
return
if expiration > 0 {
// get data after data is expired
<-time.After(expiration + time.Second*2)
err = c.Get(tc.k, &actual)
if !errors.Is(err, nscache.ErrNil) {
t.Errorf("Get: expect to get error => %v, but get %v, k=%v", nscache.ErrNil, err, tc.k)
return
}
}

// set data with long expiration time
Expand Down
1 change: 1 addition & 0 deletions memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ var (
)

func TestMemoryCache(t *testing.T) {
testutil.TestCache(t, connectionString, testutil.NoExpiration)
testutil.TestCache(t, connectionString, expiration)
}
1 change: 1 addition & 0 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
)

func TestRedisCache(t *testing.T) {
testutil.TestCache(t, connectionString, testutil.NoExpiration)
testutil.TestCache(t, connectionString, expiration)
}

Expand Down

0 comments on commit ddf2345

Please sign in to comment.