Tcp编程
.....
Redis
使⽤第三⽅开源的redis库: github.com/garyburd/redigo/redis
import(
“github.com/garyburd/redigo/redis"
)
9. 链接redis
redispackage mainimport ("fmt""github.com/garyburd/redigo/redis")func main() { c, err := redis.Dial("tcp", "localhost:6379") if err != nil { fmt.Println("conn redis failed,", err) return } defer c.Close()}
10. Set 接⼝
package mainimport ("fmt""github.com/garyburd/redigo/redis")func main() { 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 } r, err := redis.Int(c.Do("Get", "abc")) if err != nil { fmt.Println("get abc failed,", err) return } fmt.Println(r)}
11. Hash表
package mainimport ("fmt""github.com/garyburd/redigo/redis")func main() { c, err := redis.Dial("tcp", "localhost:6379") if err != nil { fmt.Println("conn redis failed,", err) return } defer c.Close() _, err = c.Do("HSet", "books", "abc", 100) if err != nil { fmt.Println(err) return } r, err := redis.Int(c.Do("HGet", "books", "abc")) if err != nil { fmt.Println("get abc failed,", err) return } fmt.Println(r)}
11. 批量Set
package mainimport ("fmt""github.com/garyburd/redigo/redis")func main() { c, err := redis.Dial("tcp", "localhost: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) }}
11. 过期时间
package mainimport ("fmt""github.com/garyburd/redigo/redis")func main() { c, err := redis.Dial("tcp", "localhost:6379") if err != nil { fmt.Println("conn redis failed,", err) return } defer c.Close() _, err = c.Do("expire", "abc", 10) if err != nil { fmt.Println(err) return }}
12. 队列操作
package mainimport ("fmt""github.com/garyburd/redigo/redis")func main() { c, err := redis.Dial("tcp", "localhost:6379") if err != nil { fmt.Println("conn redis failed,", err) return } defer c.Close() _, err = c.Do("lpush", "book_list", "abc", "ceg", 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)
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦