Created
August 2, 2021 16:48
-
-
Save maguec/27db201f9d2a07527bf4f64274300403 to your computer and use it in GitHub Desktop.
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/go-redis/redis/v8" | |
) | |
var ctx = context.Background() | |
func main() { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "localhost:6379", | |
Password: "", // blank is no password | |
DB: 0, // 0 is the default | |
MinIdleConns: 1, // make sure there are at least this many connections | |
MinRetryBackoff: 8 * time.Millisecond, //minimum amount of time to try and backupf | |
MaxRetryBackoff: 5000 * time.Millisecond, | |
MaxConnAge: 0, //3 * time.Second this will cause everyone to reconnect every 3 seconds - 0 is keep open forever | |
MaxRetries: 10, // retry 10 times : automatic reconnect if a proxy is killed | |
IdleTimeout: time.Second, | |
}) | |
// if you are using multiple go routines handle this within that routine | |
defer client.Close() | |
fmt.Println("Go Redis Example") | |
fmt.Printf("Initial Pool Stats:\n\t%+v\n", client.PoolStats()) | |
// Set a key with a TTL of 30seconds | |
err := client.Set(ctx, "key", "value", 30*time.Second).Err() | |
if err != nil { | |
panic(err) | |
} | |
// Get a key that does not exist | |
val2, err := client.Get(ctx, "key2").Result() | |
if err == redis.Nil { | |
fmt.Println("key2 does not exist") | |
} else if err != nil { | |
panic(err) | |
} else { | |
fmt.Println("key2", val2) | |
} | |
// Run a raw command | |
rawres, rawerr := client.Do(ctx, "slowlog", "reset").Result() | |
if rawerr != nil { | |
panic(rawerr) | |
} | |
fmt.Println(rawres) | |
fmt.Printf("Final Pool Stats:\n\t%+v\n", client.PoolStats()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment