Skip to content

Commit 2ba3a76

Browse files
committed
Add de redigo demo and update link
1 parent 885b1be commit 2ba3a76

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

de/05.6.md

+82-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,90 @@ redis is a key-value storage system like Memcached, that supports the string, li
1010

1111
There are some Go database drivers for redis:
1212

13+
- [https://github.com/garyburd/redigo](https://github.com/garyburd/redigo)
14+
- [https://github.com/go-redis/redis](https://github.com/go-redis/redis)
15+
- [https://github.com/hoisie/redis](https://github.com/hoisie/redis)
1316
- [https://github.com/alphazero/Go-Redis](https://github.com/alphazero/Go-Redis)
14-
- [http://code.google.com/p/tideland-rdc/](http://code.google.com/p/tideland-rdc/)
1517
- [https://github.com/simonz05/godis](https://github.com/simonz05/godis)
16-
- [https://github.com/hoisie/redis.go](https://github.com/hoisie/redis.go)
18+
19+
Let's see how to use the driver that redigo to operate on a database:
20+
21+
```Go
22+
23+
package main
24+
25+
import (
26+
"fmt"
27+
"github.com/garyburd/redigo/redis"
28+
"os"
29+
"os/signal"
30+
"syscall"
31+
"time"
32+
)
33+
34+
var (
35+
Pool *redis.Pool
36+
)
37+
38+
func init() {
39+
redisHost := ":6379"
40+
Pool = newPool(redisHost)
41+
close()
42+
}
43+
44+
func newPool(server string) *redis.Pool {
45+
46+
return &redis.Pool{
47+
48+
MaxIdle: 3,
49+
IdleTimeout: 240 * time.Second,
50+
51+
Dial: func() (redis.Conn, error) {
52+
c, err := redis.Dial("tcp", server)
53+
if err != nil {
54+
return nil, err
55+
}
56+
return c, err
57+
},
58+
59+
TestOnBorrow: func(c redis.Conn, t time.Time) error {
60+
_, err := c.Do("PING")
61+
return err
62+
},
63+
}
64+
}
65+
66+
func close() {
67+
c := make(chan os.Signal, 1)
68+
signal.Notify(c, os.Interrupt)
69+
signal.Notify(c, syscall.SIGTERM)
70+
signal.Notify(c, syscall.SIGKILL)
71+
go func() {
72+
<-c
73+
Pool.Close()
74+
os.Exit(0)
75+
}()
76+
}
77+
78+
func Get(key string) ([]byte, error) {
79+
80+
conn := Pool.Get()
81+
defer conn.Close()
82+
83+
var data []byte
84+
data, err := redis.Bytes(conn.Do("GET", key))
85+
if err != nil {
86+
return data, fmt.Errorf("error get key %s: %v", key, err)
87+
}
88+
return data, err
89+
}
90+
91+
func main() {
92+
test, err := Get("test")
93+
fmt.Println(test, err)
94+
}
95+
96+
```
1797

1898
I forked the last of these packages, fixed some bugs, and used it in my short URL service (2 million PV every day).
1999

0 commit comments

Comments
 (0)