-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* improve memory storage code and performance * improve memory storage code and performance * improve memory storage code and performance * improve memory storage code and performance * improve memory storage code and performance * improve memory storage code and performance
- Loading branch information
1 parent
5316f08
commit ce2d087
Showing
9 changed files
with
326 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package memory | ||
|
||
import "time" | ||
|
||
// Config defines the config for storage. | ||
type Config struct { | ||
// Time before deleting expired keys | ||
// | ||
// Default is 10 * time.Second | ||
GCInterval time.Duration | ||
} | ||
|
||
// ConfigDefault is the default config | ||
var ConfigDefault = Config{ | ||
GCInterval: 10 * time.Second, | ||
} | ||
|
||
// configDefault is a helper function to set default values | ||
func configDefault(config ...Config) Config { | ||
// Return default config if nothing provided | ||
if len(config) < 1 { | ||
return ConfigDefault | ||
} | ||
|
||
// Override default config | ||
cfg := config[0] | ||
|
||
// Set default values | ||
if int(cfg.GCInterval.Seconds()) <= 0 { | ||
cfg.GCInterval = ConfigDefault.GCInterval | ||
} | ||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package memory | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/gofiber/fiber/v2/utils" | ||
) | ||
|
||
var testStore = New() | ||
|
||
func Test_Storage_Memory_Set(t *testing.T) { | ||
var ( | ||
key = "john" | ||
val = []byte("doe") | ||
) | ||
|
||
err := testStore.Set(key, val, 0) | ||
utils.AssertEqual(t, nil, err) | ||
} | ||
|
||
func Test_Storage_Memory_Set_Override(t *testing.T) { | ||
var ( | ||
key = "john" | ||
val = []byte("doe") | ||
) | ||
|
||
err := testStore.Set(key, val, 0) | ||
utils.AssertEqual(t, nil, err) | ||
|
||
err = testStore.Set(key, val, 0) | ||
utils.AssertEqual(t, nil, err) | ||
} | ||
|
||
func Test_Storage_Memory_Get(t *testing.T) { | ||
var ( | ||
key = "john" | ||
val = []byte("doe") | ||
) | ||
|
||
err := testStore.Set(key, val, 0) | ||
utils.AssertEqual(t, nil, err) | ||
|
||
result, err := testStore.Get(key) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, val, result) | ||
} | ||
|
||
func Test_Storage_Memory_Set_Expiration(t *testing.T) { | ||
var ( | ||
key = "john" | ||
val = []byte("doe") | ||
exp = 1 * time.Second | ||
) | ||
|
||
err := testStore.Set(key, val, exp) | ||
utils.AssertEqual(t, nil, err) | ||
|
||
time.Sleep(1100 * time.Millisecond) | ||
} | ||
|
||
func Test_Storage_Memory_Get_Expired(t *testing.T) { | ||
var ( | ||
key = "john" | ||
) | ||
|
||
result, err := testStore.Get(key) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, true, len(result) == 0) | ||
} | ||
|
||
func Test_Storage_Memory_Get_NotExist(t *testing.T) { | ||
|
||
result, err := testStore.Get("notexist") | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, true, len(result) == 0) | ||
} | ||
|
||
func Test_Storage_Memory_Delete(t *testing.T) { | ||
var ( | ||
key = "john" | ||
val = []byte("doe") | ||
) | ||
|
||
err := testStore.Set(key, val, 0) | ||
utils.AssertEqual(t, nil, err) | ||
|
||
err = testStore.Delete(key) | ||
utils.AssertEqual(t, nil, err) | ||
|
||
result, err := testStore.Get(key) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, true, len(result) == 0) | ||
} | ||
|
||
func Test_Storage_Memory_Reset(t *testing.T) { | ||
var ( | ||
val = []byte("doe") | ||
) | ||
|
||
err := testStore.Set("john1", val, 0) | ||
utils.AssertEqual(t, nil, err) | ||
|
||
err = testStore.Set("john2", val, 0) | ||
utils.AssertEqual(t, nil, err) | ||
|
||
err = testStore.Reset() | ||
utils.AssertEqual(t, nil, err) | ||
|
||
result, err := testStore.Get("john1") | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, true, len(result) == 0) | ||
|
||
result, err = testStore.Get("john2") | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, true, len(result) == 0) | ||
} | ||
|
||
func Test_Storage_Memory_Close(t *testing.T) { | ||
utils.AssertEqual(t, nil, testStore.Close()) | ||
} | ||
|
||
func Test_Storage_Memory_Conn(t *testing.T) { | ||
utils.AssertEqual(t, true, testStore.Conn() != nil) | ||
} | ||
|
||
// go test -v -run=^$ -bench=Benchmark_Storage_Memory -benchmem -count=4 | ||
func Benchmark_Storage_Memory(b *testing.B) { | ||
keyLength := 1000 | ||
keys := make([]string, keyLength) | ||
for i := 0; i < keyLength; i++ { | ||
keys[i] = utils.UUID() | ||
} | ||
value := []byte("joe") | ||
|
||
ttl := 2 * time.Second | ||
b.Run("fiber_memory", func(b *testing.B) { | ||
d := New() | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
for _, key := range keys { | ||
d.Set(key, value, ttl) | ||
} | ||
for _, key := range keys { | ||
_, _ = d.Get(key) | ||
} | ||
for _, key := range keys { | ||
d.Delete(key) | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.