当前位置: 首页 > news >正文

外贸网站定制建站深圳网站优化教程

外贸网站定制建站,深圳网站优化教程,成都网站建设开发,十大黄金软件app免费什么是 Redis? Redis 是一个开源的内存数据库,支持多种数据结构,包括字符串、列表、集合、哈希和有序集合。由于 Redis 运行在内存中,读写速度极快,常被用于构建缓存系统、实时排行榜、会话存储和消息队列等高并发场景…

什么是 Redis?

  • Redis 是一个开源的内存数据库,支持多种数据结构,包括字符串、列表、集合、哈希和有序集合。由于 Redis 运行在内存中,读写速度极快,常被用于构建缓存系统、实时排行榜、会话存储和消息队列等高并发场景下的服务。
  • 在这篇博客中,我们将介绍如何使用 Go 语言集成 Redis,构建高效的缓存和消息队列系统。

安装 Redis

  • 安装 Redis: 你可以通过以下命令安装 Redis(适用于 Linux 和 macOS):
sudo apt-get install redis-server
  • 或者通过 Homebrew 安装:
brew install redis

启动 Redis 服务

redis-server

验证安装

  • 可以通过以下命令进入 Redis CLI,验证 Redis 是否正常运行
redis-cli

安装 Go Redis 客户端

  • 在 Go 项目中,我们将使用 go-redis/redis 这个流行的 Redis 客户端库。你可以通过以下命令安装:
go get github.com/go-redis/redis/v8

连接 redis

rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,
})

string 类型

  1. set 设置 string 的值
func TestSetKey(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})err := rdb.Set(ctx, "name", "hello world", time.Second*1000).Err()if err != nil {panic(err)}fmt.Println("设置值成功")
}
  1. get 获取 string 的值
func TestGetKey(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})result, err := rdb.Get(ctx, "name").Result()if err != nil {panic(err)}fmt.Println("result", result)
}
  1. getset 获取到的值是上一次的值
func TestGetSet(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})result, err := rdb.GetSet(ctx, "name", "hello world").Result()if err != nil {panic(err)}fmt.Println(result)
}
  1. setnx 如果值存在则不设置,如果不存在则设置
func TestSetNx(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})err := rdb.SetNX(ctx, "name", "hello set nex", time.Second*1000).Err()if err != nil {panic(err)}}
  1. mget 批量获取值
func TestMGet(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})result, err := rdb.MGet(ctx, "name", "k1", "k2").Result()if err != nil {panic(err)}fmt.Println(result)
}
  1. 批量设置
func TestMSet(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})err := rdb.MSet(ctx, "k1", "value1", "k2", "value2", "k3", "value3").Err()if err != nil {panic(err)}}
  1. 自增
// 自增
func TestIncrBy(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})result, err := rdb.IncrBy(ctx, "money", 1).Result()if err != nil {panic(err)}fmt.Println(result)
}
  1. 自减
func TestDecrBy(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})val, err := rdb.DecrBy(ctx, "money", 1).Result()if err != nil {panic(err)}fmt.Println(val)
}
  1. 删除
func TestDel(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})err := rdb.Del(ctx, "k1").Err()if err != nil {panic(err)}
}
  1. 设置过期时间
func TestExpire(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})rdb.Expire(ctx, "key2", 1000*time.Second)
}

哈希类型

  1. HSet 设置哈希值
func TestHSet(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})result, err := rdb.Ping(ctx).Result()if err != nil {panic(err)}t.Log(result)err = rdb.HSet(ctx, "user", "name", "张德志").Err()if err != nil {panic(err)}t.Log("设置hash成功")
}
  1. HGet 获取哈希值
func TestHGet(t *testing.T) {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err := rdb.Ping(ctx).Result()if err != nil {panic(err)}t.Logf("连接数据库成功")result, err1 := rdb.HGet(ctx, "user", "name").Result()if err1 != nil {panic(err1)}t.Logf("获取值:%s", result)
}
  1. TestHGetAll 获取所有哈希值
func TestHGetAll(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379",DB:   0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}t.Log("连接数据库成功")result, err1 := rdb.HGetAll(ctx, "user").Result()if err1 != nil {panic(err1)}t.Log(result)
}
  1. HIncrBy 哈希累加
func TestHIncrBy(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}t.Log("数据库连接成功")count, err1 := rdb.HIncrBy(ctx, "user", "count", 2).Result()if err1 != nil {panic(err1)}t.Log(count)
}
  1. HKeys 获取所有 keys
func TestHKeys(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}t.Log("连接数据库成功")keys, err1 := rdb.HKeys(ctx, "user").Result()if err1 != nil {panic(err)}t.Log(keys)
}
  1. HLen 查询字段数量
func TestHLen(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}t.Log("连接数据库成功")result, err1 := rdb.HLen(ctx, "user").Result()if err1 != nil {panic(err1)}t.Log(result)}
  1. HMGet 批量获取
func TestMGet(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}result, err1 := rdb.HMGet(ctx, "user", "name", "count").Result()if err1 != nil {panic(err)}t.Log(result)
}
  1. HMSet 批量设置
func TestHMSet(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}data := make(map[string]interface{})data["name"] = "周华建"data["age"] = 44data["gender"] = "男"err = rdb.HMSet(ctx, "user", data).Err()if err != nil {panic(err)}
}
  1. HDel 删除值
func TestHDel(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.HDel(ctx, "user", "name").Err()if err != nil {panic(err)}}
  1. 检测是否存在
func TestHExists(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379",DB:   0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}result, err1 := rdb.HExists(ctx, "user", "name").Result()if err1 != nil {panic(err1)}t.Log(result)
}

List 类型

  1. TestLPush 左侧插入
func TestLPush(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.LPush(ctx, "key", 1, 2, 3, 4, 5).Err()if err != nil {panic(err)}t.Log("插入成功")
}
  1. 判断集合左侧是否可以插入,如果存在则不插入,如果不存在则插入
func TestLPushX(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.LPushX(ctx, "key", 6, 7, 8).Err()if err != nil {panic(err)}
}
  1. 从右则删除一个值并返回删除后的值
func TestRPop(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}val, err1 := rdb.RPop(ctx, "key").Result()if err1 != nil {panic(err1)}t.Log(val)
}
  1. RPush 从列表右则插入值
func TestRPush(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.RPush(ctx, "key", 12).Err()if err != nil {panic(err)}
}
  1. LPop 从左侧删除
func TestLPop(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}result, err1 := rdb.LPop(ctx, "key").Result()if err1 != nil {panic(err1)}fmt.Println(result)
}
  1. LLen 获取集合的长度
func TestLLen(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}result, err1 := rdb.LLen(ctx, "key").Result()if err1 != nil {panic(err1)}t.Log(result)
}
  1. 遍历集合
func TestLRange(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}result, err1 := rdb.LRange(ctx, "key", 0, -1).Result()if err1 != nil {panic(err1)}t.Log(result)}
  1. 删除数据
func TestLRem(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}t.Log("数据库连接成功")err = rdb.LRem(ctx, "key", 0, -1).Err()if err != nil {panic(err)}t.Log("删除成功")}
  1. 获取值的索引
func TestLIndex(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379",DB:   0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}val, err := rdb.LIndex(ctx, "key", 1).Result()if err != nil {panic(err)}t.Log(val)
}

set 集合

  1. sadd 添中集合
func TestSAdd(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.SAdd(ctx, "set", 100).Err()if err != nil {panic(err)}t.Log("添加集合成功")
}
  1. scard 获取集合元素个数
func TestSCard(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379",DB:   0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}t.Log("连接数据库成功")size, err := rdb.SCard(ctx, "set").Result()if err != nil {panic(err)}t.Log(size)
}

3.sIsmember 判断元素是否在集合中

func TestSIsMember(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}ok, _ := rdb.SIsMember(ctx, "key", 100).Result()if !ok {t.Log("集合不含令指定元素")return}t.Log("集合包含指定元素")
}
  1. smembers 获取集合中所有的元素
func TestSMembers(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}values, err1 := rdb.SMembers(ctx, "set").Result()if err1 != nil {panic(err1)}t.Log(values)
}
  1. srem 删除集合中元素
func TestSRem(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.SRem(ctx, "set", 100).Err()if err != nil {panic(err)}t.Log("删除成功")
}
  1. SPop 随机删除并返回删除的值
func TestSPop(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}val, _ := rdb.SPop(ctx, "set").Result()t.Log(val)vals, _ := rdb.SPopN(ctx, "set", 5).Result()t.Log(vals)
}

可排序集合

  1. zadd 添加一个或多个元素到集合,如果元素已经存在则更新分数
func TestZAdd(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379",DB:   0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.ZAdd(ctx, "zAdd", redis.Z{Score: 2.5, Member: "张德志"}).Err()if err != nil {panic(err)}t.Log("插入成功")
}
  1. zcard 返回集合元素个数
func TestZCard(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}size, err1 := rdb.ZCard(ctx, "zAdd").Result()if err1 != nil {panic(err)}t.Log(size)}
  1. zCount 获取某个区间的值
func TestZCount(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}size, err1 := rdb.ZCount(ctx, "zAdd", "1", "5").Result()if err1 != nil {panic(err1)}t.Log(size)}
  1. ZIncrBy 增加元素的分数
func TestZIncrBy(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.ZIncrBy(ctx, "zAdd", 2, "张德志").Err()if err != nil {panic(err)}
}
  1. zrange 返回集合中某个索引范围的元素
func TestZRange(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}result, err1 := rdb.ZRange(ctx, "zAdd", 0, -1).Result()if err1 != nil {panic(err1)}t.Log(result)}
  1. ZRangeByScore 根据分数范围返回集合元素,元素根据分数从小到大排序,支持分页
func TestZRangeByScore(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}opt := redis.ZRangeBy{Min:    "2",Max:    "1000",Offset: 0,Count:  5,}vals, err1 := rdb.ZRangeByScore(ctx, "set", &opt).Result()if err1 != nil {panic(err1)}t.Log(vals)
}
  1. 根据指定 key 删除元素
func TestZRem(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.ZRem(ctx, "zAdd", "张德志").Err()if err != nil {panic(err)}t.Log("删除成功")
}
  1. ZRemRangeByRank 根据索引范围删除元素
func TestZRemRangeByRank(t *testing.T) {var err errorctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "",DB:       0,})_, err = rdb.Ping(ctx).Result()if err != nil {panic(err)}err = rdb.ZRemRangeByRank(ctx, "zAdd", 0, 1).Err()if err != nil {panic(err)}t.Log("删除成功")
}
相关链接

演示地址
获取更多
源码地址

http://www.yayakq.cn/news/251942/

相关文章:

  • 做网站什么公司app项目开发教程
  • 做网站软件 wordpage网站建设 学生作业
  • 中国做国外的网站旅游做攻略的网站有哪些
  • 网站建设规划表做网站图片大会导致慢
  • 家具网站首页设计126企业邮箱入口
  • 北京做药流凤凰网站wordpress .ht
  • 南京市住房和城乡建设部网站做网站的教学视频
  • 网站系统规划报告phpcms移动端网站怎么做
  • 建网站联系电话怎么把网站封包做app
  • 戴尔的网站建设目标做网站使网页不居中
  • 自己做网站需要什么软件下载更改wordpress传文件尺寸
  • 公司网站制作开发公司网络科技网站有哪些方面
  • 长治专业做网站贵阳网站设计zu97
  • 淘宝客如何做网站商城网站开发需求
  • wordpress怎么加js文件龙岗网站优化培训
  • 网站标签中的图片怎么做的崇左做网站公司
  • 怎么查询网站的服务器在哪里wordpress文章如何备份
  • 网站设计的目标成都专业的网站建设公司
  • 电影网站虚拟主机和vps服务质量好的外贸营销系统
  • 河北网站建设推广公司seo对各类网站的作用
  • 江门市网站建设 熊掌号神箭手wordpress免费吗
  • 汕头网站推广多少钱php网站 php有什么用
  • 页面设计多少钱seo教程视频论坛
  • 网站维护计划wordpress一键分享微博
  • 化工网站开发香河做网站
  • 为每个中小学建设网站上海网站建设微信开发
  • 网站模板素材下载哪个企业提供电子商务网站建设外包
  • 网站建设管理典型经验材料百度 网站 移动端
  • 长春市快速建站网站阳泉企业网站建设公司
  • 发号网站源码cms做网站容易不