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() }