diff --git a/en/05.6.md b/en/05.6.md index 08b0d2c7..05f023ce 100644 --- a/en/05.6.md +++ b/en/05.6.md @@ -15,6 +15,84 @@ There are some Go database drivers for redis: - [https://github.com/simonz05/godis](https://github.com/simonz05/godis) - [https://github.com/hoisie/redis.go](https://github.com/hoisie/redis.go) +Let's see how to use the driver that redigo to operate on a database: +```Go + + package main + + import ( + "fmt" + "github.com/garyburd/redigo/redis" + "os" + "os/signal" + "syscall" + "time" + ) + + var ( + Pool *redis.Pool + ) + + func init() { + redisHost := ":6379" + Pool = newPool(redisHost) + close() + } + + func newPool(server string) *redis.Pool { + + return &redis.Pool{ + + MaxIdle: 3, + IdleTimeout: 240 * time.Second, + + Dial: func() (redis.Conn, error) { + c, err := redis.Dial("tcp", server) + if err != nil { + return nil, err + } + return c, err + }, + + TestOnBorrow: func(c redis.Conn, t time.Time) error { + _, err := c.Do("PING") + return err + }, + } + } + + func close() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + signal.Notify(c, syscall.SIGTERM) + signal.Notify(c, syscall.SIGKILL) + go func() { + <-c + Pool.Close() + os.Exit(0) + }() + } + + func Get(key string) ([]byte, error) { + + conn := Pool.Get() + defer conn.Close() + + var data []byte + data, err := redis.Bytes(conn.Do("GET", key)) + if err != nil { + return data, fmt.Errorf("error get key %s: %v", key, err) + } + return data, err + } + + func main() { + test, err := Get("test") + fmt.Println(test, err) + } + +``` + I forked the last of these packages, fixed some bugs, and used it in my short URL service (2 million PV every day). - [https://github.com/astaxie/goredis](https://github.com/astaxie/goredis) diff --git a/zh/01.4.md b/zh/01.4.md index 2f9d5c64..f52a6042 100644 --- a/zh/01.4.md +++ b/zh/01.4.md @@ -114,7 +114,7 @@ 接下来就开始讲如何安装,下载[Sublime](http://www.sublimetext.com/) - 根据自己相应的系统下载相应的版本,然后打开Sublime,对于不熟悉Sublime的同学可以先看一下这篇文章[Sublime Text 2 入门及技巧](http://lucifr.com/139225/sublime-text-2-tricks-and-tips/) + 根据自己相应的系统下载相应的版本,然后打开Sublime,对于不熟悉Sublime的同学可以先看一下这篇文章[Sublime Text 全程指南](http://blog.jobbole.com/88648/)或者[sublime text3入门教程](http://blog.csdn.net/sam976/article/details/52076271) 1. 打开之后安装 Package Control:Ctrl+` 打开命令行,执行如下代码: @@ -406,7 +406,7 @@ Plugin 'Valloric/YouCompleteMe' 5. 恭喜你,安装完成,你现在可以使用`:e main.go`体验一下开发Go的乐趣。 -更多VIM 设定, 可参考[链接](http://monnand.me/p/vim-golang-environment/zhCN/) +更多VIM 设定, 可参考[链接](http://www.cnblogs.com/witcxc/archive/2011/12/28/2304704.html) ## Emacs Emacs传说中的神器,她不仅仅是一个编辑器,它是一个整合环境,或可称它为集成开发环境,这些功能如让使用者置身于全功能的操作系统中。 diff --git a/zh/05.6.md b/zh/05.6.md index 6b8ef977..9b43866c 100644 --- a/zh/05.6.md +++ b/zh/05.6.md @@ -15,7 +15,85 @@ Go目前支持redis的驱动有如下 - https://github.com/simonz05/godis - https://github.com/hoisie/redis.go -目前我fork了最后一个驱动,更新了一些bug,目前应用在我自己的短域名服务项目中(每天200W左右的PV值) +我以redigo驱动为例来演示如何进行数据的操作: +```Go + + package main + + import ( + "fmt" + "github.com/garyburd/redigo/redis" + "os" + "os/signal" + "syscall" + "time" + ) + + var ( + Pool *redis.Pool + ) + + func init() { + redisHost := ":6379" + Pool = newPool(redisHost) + close() + } + + func newPool(server string) *redis.Pool { + + return &redis.Pool{ + + MaxIdle: 3, + IdleTimeout: 240 * time.Second, + + Dial: func() (redis.Conn, error) { + c, err := redis.Dial("tcp", server) + if err != nil { + return nil, err + } + return c, err + }, + + TestOnBorrow: func(c redis.Conn, t time.Time) error { + _, err := c.Do("PING") + return err + }, + } + } + + func close() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + signal.Notify(c, syscall.SIGTERM) + signal.Notify(c, syscall.SIGKILL) + go func() { + <-c + Pool.Close() + os.Exit(0) + }() + } + + func Get(key string) ([]byte, error) { + + conn := Pool.Get() + defer conn.Close() + + var data []byte + data, err := redis.Bytes(conn.Do("GET", key)) + if err != nil { + return data, fmt.Errorf("error get key %s: %v", key, err) + } + return data, err + } + + func main() { + test, err := Get("test") + fmt.Println(test, err) + } + +``` + +另外以前我fork了最后一个驱动,更新了一些bug,目前应用在我自己的短域名服务项目中(每天200W左右的PV值) https://github.com/astaxie/goredis