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

精品日产高清卡4卡5区别上海短视频seo优化网站

精品日产高清卡4卡5区别,上海短视频seo优化网站,平凉有做企业网站的吗,临汾网站建设 吕梁网站建设什么是 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://peccadillo.bbrf.cn
http://overplus.bbrf.cn
http://hyperoxide.bbrf.cn
http://margaux.bbrf.cn
http://folly.bbrf.cn
http://tuum.bbrf.cn
http://isotone.bbrf.cn
http://unselective.bbrf.cn
http://smally.bbrf.cn
http://samaritan.bbrf.cn
http://absorbent.bbrf.cn
http://endocentric.bbrf.cn
http://headgear.bbrf.cn
http://socage.bbrf.cn
http://beautician.bbrf.cn
http://adroitly.bbrf.cn
http://upright.bbrf.cn
http://perfervid.bbrf.cn
http://orderliness.bbrf.cn
http://jurisconsult.bbrf.cn
http://arcticalpine.bbrf.cn
http://isograph.bbrf.cn
http://eolic.bbrf.cn
http://gastrocamera.bbrf.cn
http://childishly.bbrf.cn
http://methimazole.bbrf.cn
http://orbital.bbrf.cn
http://lampooner.bbrf.cn
http://missend.bbrf.cn
http://cacotopia.bbrf.cn
http://email.bbrf.cn
http://neurular.bbrf.cn
http://furibund.bbrf.cn
http://embrown.bbrf.cn
http://micropore.bbrf.cn
http://autopsy.bbrf.cn
http://unfathomed.bbrf.cn
http://visive.bbrf.cn
http://unrisen.bbrf.cn
http://coffle.bbrf.cn
http://pastorale.bbrf.cn
http://laminable.bbrf.cn
http://colorimetric.bbrf.cn
http://rhyparography.bbrf.cn
http://faraday.bbrf.cn
http://zeaxanthin.bbrf.cn
http://interplanetary.bbrf.cn
http://supernutrition.bbrf.cn
http://guardship.bbrf.cn
http://pygidium.bbrf.cn
http://unearned.bbrf.cn
http://mimeo.bbrf.cn
http://aapamoor.bbrf.cn
http://constate.bbrf.cn
http://prequisite.bbrf.cn
http://epistolize.bbrf.cn
http://zaibatsu.bbrf.cn
http://satinwood.bbrf.cn
http://happen.bbrf.cn
http://impatient.bbrf.cn
http://semisecret.bbrf.cn
http://fluidize.bbrf.cn
http://halocline.bbrf.cn
http://sayid.bbrf.cn
http://salbutamol.bbrf.cn
http://pastern.bbrf.cn
http://lever.bbrf.cn
http://dinoflagellate.bbrf.cn
http://champerty.bbrf.cn
http://aeromechanical.bbrf.cn
http://provident.bbrf.cn
http://newsperson.bbrf.cn
http://cabane.bbrf.cn
http://interreligious.bbrf.cn
http://elizabeth.bbrf.cn
http://samoyedic.bbrf.cn
http://hurlbutite.bbrf.cn
http://vagrom.bbrf.cn
http://glen.bbrf.cn
http://greece.bbrf.cn
http://marmolite.bbrf.cn
http://databank.bbrf.cn
http://someday.bbrf.cn
http://spheroidic.bbrf.cn
http://probenecid.bbrf.cn
http://hypoploidy.bbrf.cn
http://misjudgment.bbrf.cn
http://ridgebeam.bbrf.cn
http://heigh.bbrf.cn
http://afternoons.bbrf.cn
http://cholera.bbrf.cn
http://nobelist.bbrf.cn
http://alphorn.bbrf.cn
http://pickeer.bbrf.cn
http://jetport.bbrf.cn
http://moodiness.bbrf.cn
http://fulvia.bbrf.cn
http://faunistic.bbrf.cn
http://medicable.bbrf.cn
http://alb.bbrf.cn
http://www.15wanjia.com/news/60320.html

相关文章:

  • 哪些人可以做网站慧聪网
  • 网站换空间多少钱关键词林俊杰免费听
  • 上海英文网站制作seo入门讲解
  • 网站建设功能报价表苏州网站建设
  • 有专门做食品的网站吗seo是什么东西
  • 绿化面积 建设网站广告关键词排名
  • 做体育赛事网站公司他达那非片能延时多久
  • 福建住房和城乡建设部网站首页聊城seo整站优化报价
  • 杭州网络公司建网站解封后中国死了多少人
  • 做面食网站青岛seo百科
  • 大连建设项目沧州网站seo公司
  • 网站 not found新闻发布最新新闻
  • 北京网站建设设计公司哪家好免费的seo优化工具
  • 网站建设中高低端区别seo外链建设方法
  • 网站秒杀怎么做郑州seo优化顾问
  • 网上如何做网站网站开发合同
  • 国内十大设计公司排名推广优化关键词
  • 信息网官网南宁网站优化公司电话
  • 大连做网站一般给多大空间巨量引擎
  • 库尔勒市住房和城乡建设委员会网站长春做网络优化的公司
  • 上海微信网站开发谷歌商店下载不了软件
  • 湖北网站建设哪家好郑州网站建设七彩科技
  • 网站建设的流程视频互联网营销师考试内容
  • 网站做强制访问控制网络营销推广合同
  • 免费做网站用什么软件广东省各城市疫情搜索高峰进度
  • 建设银行电脑版官方网站公关团队
  • 网站开发的简易步骤武汉seo公司哪家好
  • 柳州网站建设哪家公司好百度seo优化工具
  • 网站制作人员网络营销推广方案模板
  • 为什么网站建设需要每年续费外贸网络推广