Skip to content

Commit 030a8f7

Browse files
committed
Add other redigo demo
1 parent 2ba3a76 commit 030a8f7

File tree

3 files changed

+248
-10
lines changed

3 files changed

+248
-10
lines changed

Diff for: es/05.6.md

+82-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,90 @@ Al ser el lenguaje C del siglo XXI, Go tiene soporte para bases de datos NoSQL,
99
redis es un sistema de almacenamiento llave valor como Memcached, que soporta los tipos de cadenas, listas conjuntos y conjuntos ordenados.
1010

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

1897
Realicé una bifurcación de el último de estos paquetes, arreglé algunos servicios y lo usé en mi sistema de acortado de urls (2 millones de peticiones por día).
1998

Diff for: ja/05.6.md

+84-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,90 @@ redisはkey-valueを保存するシステムです。Memcachedに似ていて、
99
現在redisが最もよく使われているのは新浪のマイクロブログプラットフォームでしょう。その次にFacebookに買収された画像フォーラムであるinstagramがあります。その他有名な[インターネット企業](http://redis.io/topics/whos-using-redis)もそうです。
1010

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

1797
現在私がforkした最新のドライバではいくつかのbugが修正されています。現在私自身の短縮ドメイン名サービスのプロジェクトの中で使用されています。(毎日200WぐらいのPV数があります。)
1898

Diff for: pt-br/05.6.md

+82-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,90 @@ As the C language of the 21st century, Go has good support for NoSQL databases,
99
redis is a key-value storage system like Memcached, that supports the string, list, set and zset(ordered set) value types.
1010

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

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

0 commit comments

Comments
 (0)