diff --git a/memcache/README.md b/memcache/README.md index ea9d2c6e..a41020a6 100644 --- a/memcache/README.md +++ b/memcache/README.md @@ -26,9 +26,7 @@ store := memcache.New() // Initialize custom config store := memcache.New(memcache.Config{ - Servers: "localhost:11211", - Timeout: 100 * time.Millisecond, - MaxIdleConns: 10, + Servers: "localhost:11211", }) ``` @@ -40,19 +38,6 @@ type Config struct { // // Optional. Default is "127.0.0.1:11211" Servers string - - // The socket read/write timeout. - // - // Optional. Default is 100 * time.Millisecond - Timeout time.Duration - - // The maximum number of idle connections that will be maintained per address. - // - // Consider your expected traffic rates and latency carefully. This should - // be set to a number higher than your peak parallel requests. - // - // Optional. Default is 2 - MaxIdleConns int } ``` @@ -60,7 +45,5 @@ type Config struct { ```go var ConfigDefault = Config{ Servers: "localhost:11211", - Timeout: 100 * time.Millisecond, - MaxIdleConns: 2, } ``` diff --git a/memcache/config.go b/memcache/config.go index cdf0778e..9dbd9a46 100644 --- a/memcache/config.go +++ b/memcache/config.go @@ -13,7 +13,7 @@ type Config struct { // The socket read/write timeout. // // Optional. Default is 100 * time.Millisecond - Timeout time.Duration + timeout time.Duration // The maximum number of idle connections that will be maintained per address. // @@ -21,14 +21,14 @@ type Config struct { // be set to a number higher than your peak parallel requests. // // Optional. Default is 2 - MaxIdleConns int + maxIdleConns int } // ConfigDefault is the default config var ConfigDefault = Config{ Servers: "localhost:11211", - Timeout: 100 * time.Millisecond, - MaxIdleConns: 2, + timeout: 100 * time.Millisecond, + maxIdleConns: 2, } // Helper function to set default values @@ -45,12 +45,12 @@ func configDefault(config ...Config) Config { if len(cfg.Servers) < 1 { cfg.Servers = ConfigDefault.Servers } - if int(cfg.Timeout) == 0 { - cfg.Timeout = ConfigDefault.Timeout - } - if cfg.MaxIdleConns == 0 { - cfg.MaxIdleConns = ConfigDefault.MaxIdleConns - } + // if int(cfg.Timeout) == 0 { + // cfg.Timeout = ConfigDefault.Timeout + // } + // if cfg.MaxIdleConns == 0 { + // cfg.MaxIdleConns = ConfigDefault.MaxIdleConns + // } return cfg } diff --git a/memcache/memcache.go b/memcache/memcache.go index 9d1c2c98..b1a210d6 100644 --- a/memcache/memcache.go +++ b/memcache/memcache.go @@ -1,6 +1,7 @@ package memcache import ( + "errors" "strings" "sync" "time" @@ -15,6 +16,9 @@ type Storage struct { items *sync.Pool } +// Common storage errors +var ErrNotExist = errors.New("key does not exist") + // New creates a new storage func New(config ...Config) *Storage { // Set default config @@ -27,8 +31,8 @@ func New(config ...Config) *Storage { db := mc.New(serverList...) // Set options - db.Timeout = cfg.Timeout - db.MaxIdleConns = cfg.MaxIdleConns + db.Timeout = cfg.timeout + db.MaxIdleConns = cfg.maxIdleConns // Ping database to ensure a connection has been made if err := db.Ping(); err != nil { diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index d6746ede..a1a730ab 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -1,3 +1,105 @@ // +build memcache package memcache + +func Test_Redis_Set(t *testing.T) { + var ( + store = testStore + key = "john" + val = []byte("doe") + ) + + err := store.Set(key, val, 0) + utils.AssertEqual(t, nil, err) +} + +func Test_Redis_Get(t *testing.T) { + var ( + store = testStore + key = "john" + val = []byte("doe") + ) + + err := store.Set(key, val, 0) + utils.AssertEqual(t, nil, err) + + result, err := store.Get(key) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, val, result) +} + +func Test_Redis_Set_Expiration(t *testing.T) { + var ( + store = testStore + key = "john" + val = []byte("doe") + exp = 500 * time.Millisecond + ) + + err := store.Set(key, val, exp) + utils.AssertEqual(t, nil, err) + + time.Sleep(1 * time.Second) + +} + +func Test_Redis_Get_Expired(t *testing.T) { + var ( + store = testStore + key = "john" + ) + + result, err := store.Get(key) + utils.AssertEqual(t, ErrNotExist, err) + utils.AssertEqual(t, true, len(result) == 0) +} + +func Test_Redis_Get_NotExist(t *testing.T) { + var store = testStore + + result, err := store.Get("notexist") + utils.AssertEqual(t, ErrNotExist, err) + utils.AssertEqual(t, true, len(result) == 0) +} + +func Test_Redis_Delete(t *testing.T) { + var ( + store = testStore + key = "john" + val = []byte("doe") + ) + + err := store.Set(key, val, 0) + utils.AssertEqual(t, nil, err) + + err = store.Delete(key) + utils.AssertEqual(t, nil, err) + + result, err := store.Get(key) + utils.AssertEqual(t, ErrNotExist, err) + utils.AssertEqual(t, true, len(result) == 0) +} + +func Test_Redis_Clear(t *testing.T) { + var ( + store = testStore + val = []byte("doe") + ) + + err := store.Set("john1", val, 0) + utils.AssertEqual(t, nil, err) + + err = store.Set("john2", val, 0) + utils.AssertEqual(t, nil, err) + + err = store.Clear() + utils.AssertEqual(t, nil, err) + + result, err := store.Get("john1") + utils.AssertEqual(t, ErrNotExist, err) + utils.AssertEqual(t, true, len(result) == 0) + + result, err = store.Get("john2") + utils.AssertEqual(t, ErrNotExist, err) + utils.AssertEqual(t, true, len(result) == 0) +} diff --git a/memory/memory.go b/memory/memory.go index d66398be..ef6399b1 100644 --- a/memory/memory.go +++ b/memory/memory.go @@ -1,6 +1,7 @@ package memory import ( + "errors" "sync" "time" ) @@ -12,6 +13,9 @@ type Storage struct { gcInterval time.Duration } +// Common storage errors +var ErrNotExist = errors.New("key does not exist") + type entry struct { data []byte expiry int64 diff --git a/memory/memory_test.go b/memory/memory_test.go index 79560779..6c711569 100644 --- a/memory/memory_test.go +++ b/memory/memory_test.go @@ -9,181 +9,104 @@ import ( "github.com/gofiber/utils" ) -func Test_Memory_Config(t *testing.T) { - t.Parallel() - - store := New(Config{}) - - utils.AssertEqual(t, ConfigDefault.GCInterval, store.gcInterval) -} - -func Test_Memory_Set(t *testing.T) { - t.Parallel() - - store := New() - - id := "hello" - value := []byte("Hi there!") - - err := store.Set(id, value, 0) +func Test_Redis_Set(t *testing.T) { + var ( + store = testStore + key = "john" + val = []byte("doe") + ) + err := store.Set(key, val, 0) utils.AssertEqual(t, nil, err) - utils.AssertEqual(t, entry{value, 0}, store.db[id]) - } -func Test_Memory_SetExpiry(t *testing.T) { - t.Parallel() - - store := New() - - id := "hello" - value := []byte("Hi there!") - expiry := time.Second * 10 - - err := store.Set(id, value, expiry) +func Test_Redis_Get(t *testing.T) { + var ( + store = testStore + key = "john" + val = []byte("doe") + ) + err := store.Set(key, val, 0) utils.AssertEqual(t, nil, err) - now := time.Now().Unix() - fromStore, found := store.db[id] - utils.AssertEqual(t, true, found) - - delta := fromStore.expiry - now - upperBound := int64(expiry.Seconds()) - lowerBound := upperBound - 2 - - if !(delta <= upperBound && delta > lowerBound) { - t.Fatalf("Test_SetExpiry: expiry delta out of bounds (is %d, must be %d lowerBound) { - t.Fatalf("Test_SetExpiry: expiry delta out of bounds (is %d, must be %d