Created
August 2, 2023 04:05
-
-
Save thuan1412/9b8ccb2a5bc99f1c142e34cff021da63 to your computer and use it in GitHub Desktop.
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
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The two methods are equivalent in the performance