這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
Redis的Go語言驅動已經很成熟了,用redigo弄了個例子。
package main //Redis測試//author:Xiong Chuan Liang//date:2015-3-5import ("fmt""time""log""github.com/garyburd/redigo/redis")func main(){fmt.Println("Redis:")conn,err := redisConn("","","6");if err != nil {log.Fatal("Error: ", err)}test(conn)}func test(conn *RedisConn) {conn.Do("SET","xxx",1)if xxx,err :=redis.Int(conn.Do("GET","xxx")); err == nil {fmt.Println("xxx:",xxx)}conn.FlushClose()}////////////////////////////////////////////////////////////////type RedisConn struct {dbid stringredis.Conn}func (r *RedisConn)FlushClose() error {if r.dbid != "" {if _, err := r.Conn.Do("SELECT", r.dbid);err != nil {return nil}}if _, err := r.Conn.Do("FLUSHDB");err != nil {return err}return r.Conn.Close()}func (r *RedisConn)Close() error {return r.Conn.Close()}func redisConn(host,password,db string) (*RedisConn, error) {if host == "" {host = ":6379"}//conn, err := redis.Dial( "tcp", host)conn , err := redis.DialTimeout("tcp", host, 0, 1*time.Second, 1*time.Second)if err != nil {return nil, err}if password != "" {if _, err := conn.Do("AUTH", password); err != nil {conn.Close()return nil, err}}if db != "" {if _, err := conn.Do("SELECT", db);err != nil {conn.Close()return nil, err}}return &RedisConn{dbid:db,Conn: conn}, nil}
可以看到使用非常方便。
MAIL:xcl_168@aliyun.com
BLOG:http://blogcsdn.net/xcl168