Golang之redis
阿新 • • 發佈:2018-01-24
lis print 初始 nload col val package con tar
redis是個開源的高性能的key-value的內存數據庫,可以把它當成遠程的數據結構。 支持的value類型非常多,比如string、list(鏈表)、set(集合)、 hash表等等 redis性能非常高,單機能夠達到15w qps,通常適合做緩存。
使用第三方開源的redis庫: github.com/garyburd/redigo/redis import( "github.com/garyburd/redigo/redis" )
1、windows安裝redis
下載安裝 https://github.com/MicrosoftArchive/redis
解壓,啟動,如下
1
2、linxu安裝redis
https://redis.io/download
3、連接redis
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func main(){ c,err:=redis.Dial("tcp","127.0.0.1:6379") if err!=nil{ fmt.Println("conn redis failed,",err) return } //fmt.Println("連接成功",c)defer c.Close() }
4、set,get,設置鍵值,取得鍵值
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func main() { //p是指針類型int var p *int var a int p = &a //p變量是個指針,指向a的內存地址 *p = 0 c, err := redis.Dial("tcp", "localhost:6379") if err != nil {//錯誤處理 fmt.Println("conn redis failed,", err) return } defer c.Close() //設置值 _, err = c.Do("Set", "abc", 100) if err != nil { fmt.Println(err) return } //從redis取值,取int類型的值 r, err := redis.Int(c.Do("Get", "abc")) if err != nil { fmt.Println("get abc failed,", err) return } fmt.Println(r) //從redis取值,取name,string類型的值 r1, err := redis.String(c.Do("Get", "name")) if err != nil { fmt.Println("get abc failed,", err) return } fmt.Println(r1) }
5、hash表設置鍵值,取鍵值
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func main() { c, err := redis.Dial("tcp", "127.0.0.1:6379") if err != nil { fmt.Println("conn redis failed,", err) return } defer c.Close() //設置hash _, err = c.Do("HSet", "books", "abc", 100) if err != nil { fmt.Println(err) return } //取得hash r, err := redis.Int(c.Do("HGet", "books", "abc")) if err != nil { fmt.Println("get abc failed,", err) return } fmt.Println(r) }
6、批量set鍵值
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func main() { //建立連接 c, err := redis.Dial("tcp", "127.0.0.1:6379") if err != nil { fmt.Println("conn redis failed,", err) return } defer c.Close() _, err = c.Do("MSet", "abc", 100, "efg", 300) if err != nil { fmt.Println(err) return } r, err := redis.Ints(c.Do("MGet", "abc", "efg")) if err != nil { fmt.Println("get abc failed,", err) return } for _, v := range r { fmt.Println(v) } }
7、設置過期時間
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func main() { c, err := redis.Dial("tcp", "127.0.0.1:6379") if err != nil { fmt.Println("conn redis failed,", err) } defer c.Close() _, err = c.Do("expire", "abc", 10) if err != nil { fmt.Println(err) return } }
8.list隊列操作
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func main() { c, err := redis.Dial("tcp", "127.0.0.1:6379") if err != nil { fmt.Println("conn redis failed,", err) return } defer c.Close() _, err = c.Do("lpush", "book_list", "abc", "efg", 300) if err != nil { fmt.Println(err) return } r, err := redis.String(c.Do("lpop", "book_list")) if err != nil { fmt.Println("get abc failed,", err) return } fmt.Println(r) }
9、redis連接池pool
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) //poll是指針 var pool *redis.Pool //初始化執行函數 func init() { pool = &redis.Pool{ MaxIdle: 16, MaxActive: 0, IdleTimeout: 300, Dial: func() (redis.Conn, error) { return redis.Dial("tcp", "127.0.0.1:6379") }, } } func main() { c := pool.Get() defer c.Close() _, err := c.Do("Set", "abc", 100) if err != nil { fmt.Println(err) return } r, err := redis.Int(c.Do("Get", "abc")) if err != nil { fmt.Println("get abc failed,", err) return } fmt.Println(r) pool.Close() }
Golang之redis