Created
August 29, 2024 16:01
-
-
Save maguec/b61c4eef45065a45312e598cce347824 to your computer and use it in GitHub Desktop.
Client Side caching example
This file contains hidden or 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/rueian/rueidis" | |
| //"github.com/jamiealquiza/tachymeter" | |
| ) | |
| func main() { | |
| client, err := rueidis.NewClient(rueidis.ClientOption{ | |
| InitAddress: []string{"10.0.0.X:6379"}, | |
| //InitAddress: []string{"10.0.0.X:6379"}, | |
| //Password: "password"} | |
| }) | |
| if err != nil { | |
| panic(err) | |
| } | |
| defer client.Close() | |
| cmd := client.B() | |
| ctx := context.Background() | |
| for i := 0; i < 1000000; i++ { | |
| keyname := fmt.Sprintf("MYNEWHASH%d", i) | |
| // HSET myhash f v | |
| _ = client.Do(ctx, cmd.Hset().Key(keyname).FieldValue().FieldValue("f", "v").Build()).Error() | |
| // HGETALL myhash | |
| startTime := time.Now() | |
| resp := client.DoCache(ctx, cmd.Hgetall().Key(keyname).Cache(), time.Minute) | |
| fmt.Printf("%+v %+v\n", resp.IsCacheHit(), time.Since(startTime).Nanoseconds()) | |
| // cache hit on client side | |
| startTime = time.Now() | |
| resp = client.DoCache(ctx, cmd.Hgetall().Key(keyname).Cache(), time.Minute) | |
| fmt.Printf("%+v %+v\n", resp.IsCacheHit(), time.Since(startTime).Nanoseconds()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment