In memory key value store application with API service
go get -u github.com/batuhankecici/keyvaluestorepackage main
import (
kvstore "github.com/batuhankecici/keyvaluestore"
)
func main() {
// create new in memory store
ims := kvstore.store.NewInMemoryStore()
// create http handler
h := kvstore.transport.CreateHTTPHandler(ims)
}Get handler response key value pair from memory
localhost:****/getSet handler sets key value pair to memory
localhost:****/setDelete handler deletes key value pair to memory
localhost:****/deleteGetall handler gets all key value pair in memory
localhost:****/getallTest Codes:
func BenchmarkCreateHttpHandler(b *testing.B) {
ims := inMemoryStore.NewInMemoryStore()
for i := 0; i < b.N; i++ {
CreateHTTPHandler(ims)
}
}
func BenchmarkSet(b *testing.B) {
ims := NewInMemoryStore()
k := "batuhan"
v := "kecici"
setValueReq := service.SetValueRequest{
Key: k,
Value: v,
}
for i := 0; i < b.N; i++ {
ims.SetValue(setValueReq)
}
}
func BenchmarkGet(b *testing.B) {
ims := NewInMemoryStore()
k := "batuhan"
v := "kecici"
setValueReq := service.SetValueRequest{
Key: k,
Value: v,
}
getValueReq := service.GetValueRequest{
Key: k,
}
ims.SetValue(setValueReq)
for i := 0; i < b.N; i++ {
ims.GetValue(getValueReq)
}
}Results:
| Function | Time | Bytes Allocated | Objects Allocated |
|---|---|---|---|
| CreateHTTPHandler | 674.7 ns/op | 624 B/op | 7 allocs/op |
| Set | 202.1 ns/op | 64 B/op | 2 allocs/op |
| Get | 28.20 ns/op | 0 B/op | 0 allocs/op |