日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用golang的http模块构建redis读写查api

發布時間:2024/4/14 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用golang的http模块构建redis读写查api 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前沿:

? ? ? ? 這兩天試著用golang做一些高性能的api,不想把壓力到聚合在平臺的接口上。平臺因為要做很多耗時間的操作,uwsgi下會出現少許錯誤,找了一圈不知道如何解決該問題。 暫時先繞道而行,先拿簡單的接口來做測試,慢慢的把復雜的操作也遷移到golang上。

? ? ? ? ? 話說以前高性能的接口,我用的最多的方案還是nginx lua的組合,超強,大家可以看看我以前寫的nginx lua的文章,各方面沒得說。只是這段時間正在看golang 的,就試著用golang實現redis的api,先來個簡單的試試手。


里面引用的是golang自帶的http模塊,redis是自己down的。

golang redis的配置方法:

cd $GOPATH/src git clone git://github.com/alphazero/Go-Redis.git redis cd redis go install


先簡單說下,golang對于redis的操作方法:

//xiaorui.cc package main import ("os";"bufio";"log";"fmt";"redis"; ) /*hello world, redis style. */ func main () {// create the client. Here we are using a synchronous client.// Using the default ConnectionSpec, we are specifying the client to connect// to db 13 (e.g. SELECT 13), and a password of go-redis (e.g. AUTH go-redis)spec := redis.DefaultSpec().Db(13).Password("go-redis");client, e := redis.NewSynchClientWithSpec (spec);if e != nil { log.Println ("failed to create the client", e); return }key := "examples/hello/user.name";value, e := client.Get(key);if e!= nil { log.Println ("error on Get", e); return }if value == nil {fmt.Printf("\nHello, don't believe we've met before!\nYour name? ");reader:= bufio.NewReader(os.Stdin);user, _ := reader.ReadString(byte('\n'));if len(user) > 1 {user = user[0:len(user)-1];value = []byte(user);client.Set(key, value);} else {fmt.Printf ("vafanculo!\n");return;}}fmt.Printf ("Hey, ciao %s!\n", fmt.Sprintf("%s", value)); }


我寫的實例,大家看懂了后,完全可以做更多的擴展。


其實golang自帶的http很有mvc的感覺,三者做了一些分離,很像python里面的web.py tornado。。。


測試結果:

服務端的啟動

客戶端的測試


原文:http://rfyiamcool.blog.51cto.com/1030776/1380754


//xiaorui.cc package main import("fmt""net/http""io/ioutil""log""time""redis" ) //xiaorui.cc const AddForm = ` <html><body> <form method="POST" action="/add"> Name: <input type="text" name="name"> Age: <input type="text" name="age"> <input type="submit" value="Add"> </form> </body></html> ` const setform = ` <html><body> <form method="POST" action="/set"> key: <input type="text" name="key"> value: <input type="text" name="value"> <input type="submit" value="set"> </form> </body></html> ` func Handler( w http.ResponseWriter,r *http.Request ){path := r.URL.Path[1:]if path == "favicon.ico" {http.NotFound(w, r)return}if path == ""{path = "index.html"}contents,err:= ioutil.ReadFile( path )if err !=nil{fmt.Fprintf( w,"404" )return}fmt.Fprintf( w,"%s\n",contents ) }func Add( w http.ResponseWriter,r *http.Request ){name := r.FormValue("name")age := r.FormValue("age")if name == "" || age == "" {fmt.Fprint(w, AddForm)return}fmt.Fprintf(w, "Save : Your name is %s , You age is %s",name,age) } func redisset( w http.ResponseWriter,r *http.Request ){key := r.FormValue("key")value := r.FormValue("value")if key == "" || value == "" {fmt.Fprint(w, setform)return}spec := redis.DefaultSpec().Db(0).Password("");client, e := redis.NewSynchClientWithSpec (spec);if e != nil { log.Println ("服務器連接有異常", e); return }inva := []byte(value)client.Set(key, inva);fmt.Fprintf(w, "哥們,你輸入的key %s 和value %s 已經插入到redis里面了",key,key) } func redisget( w http.ResponseWriter,r *http.Request ){key := r.FormValue("key")if key == "" {fmt.Fprint(w, setform)return}spec := redis.DefaultSpec().Db(0).Password("");client, e := redis.NewSynchClientWithSpec (spec);if e != nil { log.Println ("服務器連接有異常", e); return }value, e := client.Get(key);fmt.Fprintf(w, "哥們,你要查詢的key %s 和value %s ",key,value) } func valueget(w http.ResponseWriter, r *http.Request) {params := r.URL.Query()user := params.Get("user")fmt.Fprintf(w, "you are get user %s", user) }func main(){http.HandleFunc( "/",Handler)http.HandleFunc( "/add",Add)http.HandleFunc( "/redisset",redisset)http.HandleFunc( "/redisget",redisget)http.HandleFunc( "/valueget",valueget)s := &http.Server{Addr: ":80",ReadTimeout: 30 * time.Second,WriteTimeout: 30 * time.Second,MaxHeaderBytes: 1 << 20,}log.Fatal(s.ListenAndServe()) }


對于go的基礎教程,我也寫過其他的文章,大家可以參考下。


關于Golang語言的web編程的實例及常見問題

http://rfyiamcool.blog.51cto.com/1030776/1285325


關于Go語言在服務端做Restful接口和socket通信

http://rfyiamcool.blog.51cto.com/1030776/1286372




轉載于:https://blog.51cto.com/rfyiamcool/1380754

總結

以上是生活随笔為你收集整理的使用golang的http模块构建redis读写查api的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。