summaryrefslogtreecommitdiff
path: root/redigo.go
blob: f83b1742356ad6e6a6ef54861f9713d11cd94f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main

import (
    "github.com/go-redis/redis"
    "fmt"
    "time"
)

var client *redis.Client

func init() {
    client = redis.NewClient(&redis.Options{
        Addr:         "192.168.0.16:6379",
        Password: "", // no password set
        DB:       0,  // use default DB

       // DialTimeout:  10 * time.Second,
       // ReadTimeout:  30 * time.Second,
       // WriteTimeout: 30 * time.Second,
       // PoolSize:     10,
       // PoolTimeout:  30 * time.Second,
    })
    //client.FlushDB()
}

func getRed(key string) string {
    val, err := client.Get(key).Result()
    if err == redis.Nil {
        return "nil"
    } else if err != nil {
        panic(err)
        return err.Error()
    } else {
        return val
    }
}

func setRed(key string , val string, dur int) bool {
    err := client.Set(key, val,time.Duration(dur)*time.Second).Err()
    if err != nil {
        panic(err)
        return false
    }
    return true
}

func ExampleNewClient() {
    client := redis.NewClient(&redis.Options{
    Addr:     "192.168.0.16:6379",
    Password: "", // no password set
    DB:       0,  // use default DB
})

    pong, err := client.Ping().Result()
    fmt.Println(pong, err)
    // Output: PONG <nil>
}


func ExampleClient() {
    client := redis.NewClient(&redis.Options{
        Addr:     "192.168.0.16:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })
    err := client.Set("zack", "kack", 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 exists")
    } else if err != nil {
        panic(err)
    } else {
    fmt.Println("key2", val2)
    }
// Output: key value
// key2 does not exists
}

//func main() {
    //ExampleClient()
//    if setRed("scheisse","geil",10) {
//        fmt.Println("euda")
//    }
//}