golang 链接redis Redis使用 使用第三方开源的redis库: github.com/garyburd/redigo/redis
命令行输入 :
1 go get github.com/garyburd/redigo/redis
链接Redis 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package main import ( "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 } fmt.Println("redis conn success") defer c.Close() }
String类型Set、Get操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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) }
运行结果:
1 MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
Redis被配置为保存数据库快照,但它目前不能持久化到硬盘。用来修改集合数据的命令不能用。请查看Redis日志的详细错误信息。
原因:
强制关闭Redis快照导致不能持久化。
解决方案:
运行config set stop-writes-on-bgsave-error no 命令后,关闭配置项stop-writes-on-bgsave-error解决该问题。
开启命令行新窗口 2:
链接Redis:
1 2 3 redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> config set stop-writes-on-bgsave-error no OK
返回命令行窗口 1 运行程序:
输出结果:
命令行窗口 2:
1 2 127.0.0.1:6379> get abc "100"
prev
last update time 2022-05-06
next