Last active
December 2, 2015 01:11
-
-
Save aalvesjr/cd3e00cc3d6e1634c7da to your computer and use it in GitHub Desktop.
Testing redis with Go
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
/* | |
https://golang.org/doc/code.html#GOPATH | |
definir a variavel GOPATH | |
> http://gopkg.in/redis.v3 | |
dentro do caminho definido na GOPATH, executar: | |
go get gopkg.in/redis.v3 | |
*/ | |
package main | |
import ( | |
"fmt" | |
"gopkg.in/redis.v3" | |
"time" | |
) | |
func main() { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "localhost:6379", | |
Password: "", // no password set | |
DB: 0, // use default DB | |
}) | |
pong, err := client.Ping().Result() | |
fmt.Println(pong, err) | |
// Output: PONG <nil> | |
name := "name" | |
// O terceiro parametro é o tempo para o valor expirar | |
err2 := client.Set(name, "Armando", 10*time.Second).Err() | |
if err2 != nil { | |
panic(err) | |
} | |
val, err := client.Get(name).Result() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(name, val) | |
// Output: name Armando | |
val2, err := client.Get("key2").Result() | |
if err == redis.Nil { | |
fmt.Println("key2 does not exists") | |
} else if err != nil { | |
panic(err) | |
} else { | |
fmt.Println("key2", val2) | |
} | |
// Output: key2 does not exists | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment