Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- "fmt"
- "time"
- "github.com/redis/go-redis/v9"
- )
- var S = ""
- const REDIS_HOST = "myredisisntance.com"
- var localcache = make(map[string]string)
- func main() {
- ctx := context.Background()
- rdb := redis.NewClient(&redis.Options{
- Addr: REDIS_HOST + ":6379",
- Password: "",
- DB: 0,
- })
- err := rdb.Ping(ctx).Err()
- if err != nil {
- panic(err)
- }
- err = rdb.Set(ctx, "key", "value", 0).Err()
- if err != nil {
- panic(err)
- }
- totalRedisGetTime := time.Duration(0)
- totalLocalGetTime := time.Duration(0)
- for i := 0; i < 1000; i++ {
- if i == 1 || i %100 == 0 {
- fmt.Printf("Progress: %v/1000\n", i)
- }
- totalRedisGetTime += getFromRedis(rdb, "key")
- totalLocalGetTime += getFromLocalCache("key")
- }
- fmt.Printf("Total time to get from Redis: %v\n", totalRedisGetTime)
- fmt.Printf("Total time to get from Local Cache: %v\n", totalLocalGetTime)
- }
- func getFromRedis(rdb *redis.Client, key string) time.Duration {
- start := time.Now()
- var err error
- ctx := context.Background()
- S, err = rdb.Get(ctx, key).Result()
- if err != nil {
- panic(err)
- }
- return time.Since(start)
- }
- func getFromLocalCache(key string) time.Duration {
- start := time.Now()
- S = localcache[key]
- return time.Since(start)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement