From ddf234580cc87cc74ed1d2ad11e3003e9fe15572 Mon Sep 17 00:00:00 2001 From: mstmdev Date: Sat, 10 Jun 2023 00:02:32 +0800 Subject: [PATCH] Fix set the data with no expiration is ineffective in the buntdb (#29) --- VERSION | 2 +- boltdb/boltdb_test.go | 1 + buntdb/buntdb.go | 6 +++++- buntdb/buntdb_benchmark_test.go | 12 ++++++++++++ buntdb/buntdb_test.go | 8 ++++++-- cache/cache_test.go | 1 + etcd/etcd_test.go | 1 + extension/extension_test.go | 1 + internal/testutil/testutil.go | 22 +++++++++++++++------- memory/memory_test.go | 1 + redis/redis_test.go | 1 + 11 files changed, 45 insertions(+), 11 deletions(-) diff --git a/VERSION b/VERSION index b82608c..a1c2c6a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.1.0 +v0.1.1 \ No newline at end of file diff --git a/boltdb/boltdb_test.go b/boltdb/boltdb_test.go index a2c89b7..6cd6ca7 100644 --- a/boltdb/boltdb_test.go +++ b/boltdb/boltdb_test.go @@ -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) } diff --git a/buntdb/buntdb.go b/buntdb/buntdb.go index fd53879..b7d7724 100644 --- a/buntdb/buntdb.go +++ b/buntdb/buntdb.go @@ -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 }) } diff --git a/buntdb/buntdb_benchmark_test.go b/buntdb/buntdb_benchmark_test.go index e5a7520..38604fe 100644 --- a/buntdb/buntdb_benchmark_test.go +++ b/buntdb/buntdb_benchmark_test.go @@ -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) +} diff --git a/buntdb/buntdb_test.go b/buntdb/buntdb_test.go index b4f5536..b59eb9e 100644 --- a/buntdb/buntdb_test.go +++ b/buntdb/buntdb_test.go @@ -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) { diff --git a/cache/cache_test.go b/cache/cache_test.go index bc7bbea..2ac6840 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -8,5 +8,6 @@ import ( ) func TestCache(t *testing.T) { + testutil.TestCache(t, testutil.MemoryConnectionString, testutil.NoExpiration) testutil.TestCache(t, testutil.MemoryConnectionString, testutil.DefaultExpiration) } diff --git a/etcd/etcd_test.go b/etcd/etcd_test.go index 14a6fcc..55a539a 100644 --- a/etcd/etcd_test.go +++ b/etcd/etcd_test.go @@ -13,6 +13,7 @@ var ( ) func TestEtcdCache(t *testing.T) { + testutil.TestCache(t, connectionString, testutil.NoExpiration) testutil.TestCache(t, connectionString, expiration) } diff --git a/extension/extension_test.go b/extension/extension_test.go index 7509339..b9ff378 100644 --- a/extension/extension_test.go +++ b/extension/extension_test.go @@ -20,6 +20,7 @@ func TestExtension(t *testing.T) { }{ {testutil.MemoryConnectionString}, {testutil.BuntDBConnectionString}, + {testutil.BuntDBMemoryConnectionString}, {testutil.EtcdConnectionString}, {testutil.RedisConnectionString}, } diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index f054bda..7fdfc5e 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -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://:memory:" + BuntDBConnectionString = "buntdb://buntdb.db" + // BuntDBMemoryConnectionString a buntdb memory cache driver test connection string + BuntDBMemoryConnectionString = "buntdb://:memory:" // EtcdConnectionString a etcd cache driver test connection string EtcdConnectionString = "etcd://127.0.0.1:2379?dial_timeout=5s" // RedisConnectionString a redis cache driver test connection string @@ -21,6 +23,8 @@ const ( BoltDBConnectionString = "boltdb://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 @@ -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 { @@ -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 diff --git a/memory/memory_test.go b/memory/memory_test.go index 1b10045..ab3c3bb 100644 --- a/memory/memory_test.go +++ b/memory/memory_test.go @@ -12,5 +12,6 @@ var ( ) func TestMemoryCache(t *testing.T) { + testutil.TestCache(t, connectionString, testutil.NoExpiration) testutil.TestCache(t, connectionString, expiration) } diff --git a/redis/redis_test.go b/redis/redis_test.go index db4ee5c..9442f88 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -12,6 +12,7 @@ var ( ) func TestRedisCache(t *testing.T) { + testutil.TestCache(t, connectionString, testutil.NoExpiration) testutil.TestCache(t, connectionString, expiration) }