Go語言操作Redis

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

       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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.