Last active
August 15, 2019 00:03
-
-
Save amitsaha/19c829e3b44ce7421c0a3af5d9160197 to your computer and use it in GitHub Desktop.
Redis + go
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
module github.com/amitsaha/redis-demo | |
go 1.12 | |
require github.com/go-redis/redis v6.15.2+incompatible |
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
github.com/go-redis/redis v6.15.2+incompatible h1:9SpNVG76gr6InJGxoZ6IuuxaCOQwDAhzyXg+Bs+0Sb4= | |
github.com/go-redis/redis v6.15.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= |
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 ( | |
"fmt" | |
"log" | |
"time" | |
"github.com/go-redis/redis" | |
) | |
func main() { | |
ExampleZListClient() | |
} | |
func ExampleNewClient() { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "localhost:6379", | |
Password: "password", // no password set | |
DB: 0, // use default DB | |
}) | |
start := time.Now() | |
pong, err := client.Ping().Result() | |
fmt.Println(time.Since(start)) | |
fmt.Println(pong, err) | |
// Output: PONG <nil> | |
} | |
func ExampleZListClient() { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "localhost:6379", | |
Password: "", // no password set | |
DB: 0, // use default DB | |
}) | |
tags := map[string]float64{ | |
"AFK": 3, | |
"GTG": 1, | |
"SAC": 2, | |
"BRB": 1, | |
"GTFO": 1, | |
"LOL": 1, | |
"LMAO": 1, | |
"GLHF": 1, | |
"SMH": 1, | |
"ROTFL": 1, | |
} | |
for tag, score := range tags { | |
_, err := client.ZAdd("tags", redis.Z{score, tag}).Result() | |
if err != nil { | |
log.Fatalf("Error adding %s: %v", tag, err) | |
} | |
} | |
result, err := client.ZRevRangeWithScores("tags", 0, 6).Result() | |
if err != nil { | |
log.Fatalf("Error retrieving top 5 keys: %v", err) | |
} | |
for _, zItem := range result { | |
log.Printf("%v\n", zItem) | |
} | |
} | |
func ExampleClient() { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "localhost:6379", | |
Password: "password", // no password set | |
DB: 0, // use default DB | |
}) | |
err := client.Set("key", "value", 0).Err() | |
if err != nil { | |
panic(err) | |
} | |
val, err := client.Get("key").Result() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("key", val) | |
val2, err := client.Get("key2").Result() | |
if err == redis.Nil { | |
fmt.Println("key2 does not exist") | |
} else if err != nil { | |
panic(err) | |
} else { | |
fmt.Println("key2", val2) | |
} | |
// Output: key value | |
// key2 does not exist | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment