当前位置:网站首页>[Golang数据库专题5]Golang语言操作Redis进行增删改查
[Golang数据库专题5]Golang语言操作Redis进行增删改查
2022-07-19 12:07:00 【风间净琉璃】
目录
一、环境准备(已经安装redis的直接跳过本步骤)
redis安装有在线安装和离线安装,后者针对某些内网环境部署时不能与外网通信。
这里采用docker在线安装。
1.1 安装redis
搜索镜像
输入以下指令搜索 redis 镜像
docker search redis
拉取镜像
这里安装redis 5版本
docker pull redis:5
查看本地镜像
拉取完后,查看本地镜像。
docker images
创建挂载目录
在linux中创建redis运行时的挂载目录,方便在docker容器外配置redis。
这里挂载目录分别是,可根据自己实际情况创建不同路径的挂载目录
mkdir /home/zhongqiu/redis
mkdir /home/zhongqiu/redis/data
这一步可以按需挂载 redis.conf,这里不做挂载 配置文件
运行镜像
docker run -p 6379:6379 --name redis-5 -v /home/zhongqiu/redis/data:/data -d redis:5 redis-server --appendonly yes
如果 redis.conf挂载了,写法如下:
docker run -p 6379:6379 --name redis-five -v /home/zhongqiu/redis/redis.conf:/etc/redis/redis.conf -v /home/zhongqiu/redis/data:/data -d redis:5 redis-server /etc/redis/redis.conf --appendonly yes
-p 6379:6379:把容器内的6379端口映射到宿主机6379端口
-v /home/zhongqiu/redis/redis.conf:/etc/redis/redis.conf:把宿主机配置好的redis.conf放到容器内的这个位置中
-v /home/zhongqiu/redis/data:/data:把redis持久化的数据在宿主机内显示,做数据备份
redis-server /home/zhongqiu/redis/redis.conf:这个是关键配置,让redis不是无配置启动,而是按照这个redis.conf的配置启动
redis:5 版本号
–appendonly yes:redis启动后数据持久化
查看启动
docker ps -a
二、驱动选型
Redis是一个键值对存储系统,和Memcached类似,支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。
目前应用Redis最广泛的应该是新浪微博平台,其次还有Facebook收购的图片社交网站 instagram。Go语言目前支持Redis的驱动如下:
- GitHub - gomodule/redigo: Go client for RedisGo client for Redis. Contribute to gomodule/redigo development by creating an account on GitHub.
https://github.com/gomodule/redigo gitstar 9.2k
- GitHub - go-redis/redis: Type-safe Redis client for GolangType-safe Redis client for Golang. Contribute to go-redis/redis development by creating an account on GitHub.
https://github.com/go-redis/redis gitStar 14.9k
- GitHub - hoisie/redis: A simple, powerful Redis client for GoA simple, powerful Redis client for Go. Contribute to hoisie/redis development by creating an account on GitHub.
https://github.com/hoisie/redis gitStar 589
- GitHub - alphazero/Go-Redis: Google Go Client and Connectors for RedisGoogle Go Client and Connectors for Redis. Contribute to alphazero/Go-Redis development by creating an account on GitHub.
https://github.com/alphazero/Go-Redis gitStar 440
- GitHub - insmo/godis: godis - an old Redis client for Gogodis - an old Redis client for Go. Contribute to insmo/godis development by creating an account on GitHub.
https://github.com/insmo/godis gitStar 86
本例中将采用git star最高的 go-redis/redis组件。
三、编写代码
3.1 简单读写代码(含计数)
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"time"
)
var ctx = context.Background()
func main() {
// 建立链接
rdb := redis.NewClient(&redis.Options{
Addr: "192.168.58.128:6379",
Password: "", // no password set
DB: 0, // use default DB
})
// set值
err := rdb.Set(ctx, "key", "value", 0).Err()
if err != nil {
panic(err)
}
// 获取值
val, err := rdb.Get(ctx, "key").Result()
if err != nil {
panic(err)
}
fmt.Println("key", val)
val2, err := rdb.Get(ctx, "key2").Result()
if err == redis.Nil {
fmt.Println("key2 does not exist")
} else if err != nil {
panic(err)
} else {
fmt.Println("key2", val2)
}
// 计数器
incr := rdb.Incr(ctx, "incrKeys")
fmt.Println("回调计数器加一的值:", incr)
// 设置过期为10S
rdb.SetEX(ctx, "expireTestKey", "飞飞飞", 10000)
// 休眠12S
time.Sleep(time.Duration(12) * time.Second)
fmt.Println("key = expireTestKey的值 =", rdb.Get(ctx, "expireTestKey"))
rdb.Close()
}
输出:
3.2 分布式锁
模仿两个线程抢占同一把锁,若获取到锁则进行业务操作,未获取到则不进行业务操作。
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/gofrs/uuid"
"time"
)
var ctx = context.Background()
func main() {
// 建立链接
rdb := redis.NewClient(&redis.Options{
Addr: "192.168.58.128:6379",
Password: "", // no password set
DB: 0, // use default DB
})
for i := 0; i < 2; i++ {
go func() {
v4, err := uuid.NewV4()
if err != nil {
panic(err)
}
threadId := v4.String()
fmt.Println("当前线程唯一值=", threadId)
// 这里最后一个参数代表时间,0代表值不过期,-1=keepttl,会比较时间和时间单位S的关系,最后转换成MS或者S
// 如果最后时间设置小了,可能获取到分布式锁的业务还未执行完,分布式锁就过期了(本文不探讨锁过期机制)
result, err := rdb.SetNX(ctx, "setNxTestKeyName", threadId, 10000000000).Result()
if err != nil {
panic(err)
}
if result {
fmt.Println("开始业务操作", threadId)
time.Sleep(time.Second * 4)
fmt.Println("结束业务操作", threadId)
// 这里该是删除对应线程设置的锁,这里没有考虑业务线程过期因素
value := rdb.Get(ctx, "setNxTestKeyName").Val()
fmt.Println("对应线程锁的值", value)
if err != nil {
panic(err)
}
if value == threadId {
rdb.Del(ctx, "setNxTestKeyName")
fmt.Println("删除了对应的key", threadId)
}
} else {
fmt.Println("没有获取到锁,无法进行业务操作", threadId)
}
}()
}
time.Sleep(time.Second * 20)
rdb.Close()
}
运行结果:
四、官方文档
要了解该组件,最好看第一手官方文档资料,对应操作api基本与redis指令一一对应,地址见:
https://pkg.go.dev/github.com/go-redis/redis/v8#section-readme
边栏推荐
猜你喜欢
【历史上的今天】7 月 19 日:IMAP 协议之父出生;Project Kotlin 公开亮相;CT 成像实现新突破
不等式学习笔记
(0711-0717) memorabilia of open source software security this week
Build a full stack web application framework in 1 hour, support secondary development, and update and expand at any time
IDEA中如何安装插件和宝贝插件的推荐
容斥【玲珑杯】咸鱼值
Devops has been practiced for many years. What is the most painful thing?
雲原生核心技術之:Service Mesh 的實現—— Istio
【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
随机推荐
集合之Arraylist
容斥 [Jsoi2011]分特产
CB Insights发布AI行业七大趋势:合成数据、多模态AI崛起
JSD-2204-微博项目(完结)-Day16
Silvaco二极管、三极管、CMOS的制备
IDEA中如何安装插件和宝贝插件的推荐
科技云报道:零信任和SASE有什么不一样?答案其实并不重要
请问,现在有什么短期的理财产品值得买?
Online XML to JSON tool
Socket error Event: 32 Error: 10053. Connection closing...Socket close
剑指offer题库总结(一)之数组(C语言版本)
三招从 20s 优化到 500ms
How to choose aion LX plus and velai ES6, the top products of high-end pure electric SUV?
2022.7.10-----leetcode. seven hundred and forty-one
25、继承与派生
Flink SQL configures Kafka to chain a topic with multiple partitions, and there is no error. There is no problem with a single partition
Import word document pictures kernel synchronization and mutual exclusion
Literary and artistic calculation Ji of provincial election and professional training
【BZOJ2393】Cirno的完美算数教室
Svn compares local changes relative to the previous version