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

济南网站建设vashine连锁店进销存软件

济南网站建设vashine,连锁店进销存软件,网站建设是啥工作,广州网站制作多少钱什么是 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/661174/

相关文章:

  • 个人网站免费域名企业品牌宣传型网站
  • 网站外链查询wordpress线上安装
  • jsp网站开发实例重庆秀山网站建设报价
  • 聊城商城网站建设南通市网站建设我的完
  • 开发一个网站做公司内部用wordpress 站点维护
  • 企业建设网站是网络营销吗沧州网站建设icp备
  • 上海高级网站建设成都公司网页制作联系电话
  • 做网站的相关教程做瞹瞹网站
  • 湖州吴兴建设局网站seo评测论坛
  • 自己建网站做网店江西建设推广网站
  • 每月网站开发费用制作一个网站步骤
  • 重庆专业网站开发服务漳州市住房建设局网站
  • 平台网站建设ppt网站维护教程
  • 做淘客应该知道的网站天津建设工程信息网报名
  • 国内最好用免费建站系统国家注册商标官方网
  • 中国建设银行网站个人客户企业网站策划案
  • 12306网站为什么做那么差网上书城网站系统建设
  • dede 学校网站玉林做网站公司
  • 网站改版 需求文档wordpress禁止s.w.org
  • 购物网站建设网做网站muse好还是DW好用
  • 淄博网站建设详细策划做木皮的网站
  • 网站改版与优化协议书贵阳市花溪区建设局网站
  • 如何做网站的基础免费的平面设计网站
  • 做宣传 为什么要做网站那做seo网站的公司
  • 电子商务网站的开发方式开源asp学校系统网站
  • 建设官方网站首页wordpress 友情链接页
  • 设计做图免费网站天津网站建设索王道下拉
  • 做网站机构网站怎么做的qq邮件订阅
  • 外贸英文网站制作wordpress5.0.1编辑器
  • 赤峰微网站建设怎么把网页里的视频提取出来