Skip to content

Instantly share code, notes, and snippets.

@thuan1412
Created August 2, 2023 04:05
Show Gist options
  • Save thuan1412/9b8ccb2a5bc99f1c142e34cff021da63 to your computer and use it in GitHub Desktop.
Save thuan1412/9b8ccb2a5bc99f1c142e34cff021da63 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
var ctx = context.Background()
const totalReqs = 10000
var rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 8, // use default DB
})
func init() {
// create {totalReqs} keys
for i := 0; i < totalReqs; i++ {
key := fmt.Sprintf("key:%d", i)
rdb.Set(ctx, key, fmt.Sprintf("%d", i), 10)
rdb.HSet(ctx, "key", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i))
}
}
func normalKeyGet() {
start := time.Now()
for i := 0; i < totalReqs; i++ {
key := fmt.Sprintf("key:%d", i)
rdb.Get(ctx, key)
}
fmt.Println("normalKeyGet: ", time.Since(start))
}
func hashKeyGet() {
start := time.Now()
for i := 0; i < totalReqs; i++ {
rdb.HGet(ctx, "key", fmt.Sprintf("%d", i))
}
fmt.Println("hashKeyGet: ", time.Since(start))
}
func main() {
normalKeyGet()
hashKeyGet()
}
@thuan1412
Copy link
Author

The two methods are equivalent in the performance

normalKeyGet:  370.260468ms
hashKeyGet:  376.055465ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment