关键词搜索

源码搜索 ×
×

用go来操作redis

发布2018-09-30浏览7468次

详情内容

         安装redis, 运行如下go代码:

  1. package main
  2. import (
  3. "time"
  4. "fmt"
  5. "github.com/go-redis/redis"
  6. )
  7. var Client *redis.Client
  8. func init() {
  9. Client = redis.NewClient(&redis.Options{
  10. Addr: "127.0.0.1:6379",
  11. PoolSize: 1000,
  12. ReadTimeout: time.Millisecond * time.Duration(100),
  13. WriteTimeout: time.Millisecond * time.Duration(100),
  14. IdleTimeout: time.Second * time.Duration(60),
  15. })
  16. _, err := Client.Ping().Result()
  17. if err != nil {
  18. panic("init redis error")
  19. } else {
  20. fmt.Println("init redis ok")
  21. }
  22. }
  23. func get(key string) (string, bool) {
  24. r, err := Client.Get(key).Result()
  25. if err != nil {
  26. return "", false
  27. }
  28. return r, true
  29. }
  30. func set(key string, val string, expTime int32) {
  31. Client.Set(key, val, time.Duration(expTime) * time.Second)
  32. }
  33. func main() {
  34. set("name", "x", 100)
  35. s, b := get("name")
  36. fmt.Println(s, b)
  37. }

         结果:

init redis ok
x true

   

        过期时间是100s,  过期后,get无法获取信息, 返回了nil

 

        简单, 不多说。

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载