Skip to content

Instantly share code, notes, and snippets.

@maguec
Created August 29, 2024 16:01
Show Gist options
  • Select an option

  • Save maguec/b61c4eef45065a45312e598cce347824 to your computer and use it in GitHub Desktop.

Select an option

Save maguec/b61c4eef45065a45312e598cce347824 to your computer and use it in GitHub Desktop.
Client Side caching example
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