Golang学习系列第七天:操做Redis

0   redis安装请参考Redis备忘录

1.  golang操做redis

切换到golang工做目录,新建项目redis,而后创建链接redis的文件

[root@master src]# pwd /dongguangming/goworkspace/src [root@master src]# mkdir redis [root@master src]# cd redis/ [root@master redis]# touch redis-conn.go 

编辑redis-conn.go文件 ,即html

[root@master redis]# vi  redis-conn.go

键入如下代码git

package main import ( "fmt" "github.com/go-redis/redis" ) func main() { fmt.Println("golang链接redis") client := redis.NewClient(&redis.Options{ Addr: "192.168.8.200:6379", Password: "123456", DB: 0, }) pong, err := client.Ping().Result() fmt.Println(pong, err) } 

执行以上程序github

[root@master redis]# go run redis-conn.go golang链接redis PONG <nil> 

没有报错,表示链接redis成功!!!golang

 

1.1  添加键值

经过golang设置redis键值前,请先经过redis shell查询下是否存在

[root@master ~]# redis-cli -h 192.168.8.200 -p 6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.8.200:6379> get golang (nil) 

很好键golang并不存在,返回nilredis

而后经过golang添加键值shell

package main import ( "fmt" "github.com/go-redis/redis" ) func main() { fmt.Println("golang链接redis") client := redis.NewClient(&redis.Options{ Addr: "192.168.8.200:6379", Password: "123456", DB: 0, }) pong, err := client.Ping().Result() fmt.Println(pong, err) //添加键值对 err = client.Set("golang", "yes", 0).Err() if err != nil { fmt.Println(err) } fmt.Println("键golang设置成功") } 

执行以上代码bash

[root@master redis]# go run redis-conn.go golang链接redis PONG <nil> 键golang设置成功 

最后经过shell 查看是否键是否设置成功app

[root@master ~]# redis-cli  -h 192.168.8.200 -p 6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.8.200:6379> get golang (nil) 192.168.8.200:6379> get golang "yes" 

结果代表通golang设置的键golang的值为yes生效了!!!memcached

 

1.2  获取键值

获取1.1设置的键值ui

import ( "fmt" "github.com/go-redis/redis" ) func main() { fmt.Println("golang链接redis") client := redis.NewClient(&redis.Options{ Addr: "192.168.8.200:6379", Password: "123456", DB: 0, }) pong, err := client.Ping().Result() fmt.Println(pong, err) //添加键值对 err = client.Set("golang", "yes", 0).Err() if err != nil { fmt.Println(err) } fmt.Println("键golang设置成功") //经过键查询值 val, err := client.Get("golang").Result() if err != nil { fmt.Println(err) } fmt.Println("键golang的值为: ",val) } 

执行程序,输出结果

[root@master redis]# go run redis-conn.go golang链接redis PONG <nil> 键golang设置成功 键golang的值为: yes 

其余特性自行发挥!!!

 

后记: 已把个人电子书已上传至github:https://github.com/dongguangming/dgm-collection/tree/master/%E6%95%B0%E6%8D%AE%E5%BA%93/redis

dongguagming github

后续会慢慢上传其余资料。

 

参考:

    1. Golang’s Superior Cache Solution to Memcached and Redis https://www.mailgun.com/blog/golangs-superior-cache-solution-memcached-redis/

    2. A tour of the Redis stars  https://www.compose.com/articles/a-tour-of-the-redis-stars-2/

    3. Getting Started with Redis and Go - Tutorial https://tutorialedge.net/golang/go-redis-tutorial/

    4. How to Use Redis Go Client go-redis/redis with GoLang https://kb.objectrocket.com/redis/how-to-use-redis-go-client-go-redis-redis-with-golang-592

    5. Golang: Redis cluster client example https://golangbyexample.com/golang-redis-cluster-client-example/

    6. go-redisでのRedisデータ型の扱い方 https://qiita.com/momotaro98/items/ed496ba06908b278e103

    7.  Golang / Go Crash Course 08 | Using Redis as A Cache for our REST API https://m.youtube.com/watch?v=x5GGLrTuQCA

    8. 使用 Go 语言读写Redis协议 https://colobu.com/2019/04/16/Reading-and-Writing-Redis-Protocol-in-Go/https://www.redisgreen.net/blog/reading-and-writing-redis-protocol/

    9. Distributed Locks using Golang and Redis https://kylewbanks.com/blog/distributed-locks-using-golang-and-redis

    10. Go and Compose - Redis, RethinkDB, and RabbitMQ https://www.compose.com/articles/go-and-compose-redis-rethinkdb-and-rabbitmq/

相关文章
相关标签/搜索