【go】golang redis | github.com/redis/go-redis/v9 正确打开golang的redis | go-zero redis 与simple-admin redis

Your Image Description

让我们一起支持群主维护simple-admin 社群吧!!!
不能加入星球的朋友记得来点个Star!!
https://github.com/suyuan32/simple-admin-core

一、redis框架推荐

1、go-zero redis

go-zero 本身整合了个redis框架
优势:在svc中注册后,全局直接调用,且整合了redis分布式锁

https://github.com/zeromicro/go-zero/core/stores/redis

2、redis官方版本

推荐使用 v9版本
优势:使用更加灵活,可以控制返回值

https://github.com/redis/go-redis

二、2个框架与redis

官方文档:

https://redis.uptrace.dev/guide/go-redis.html#connecting-to-redis-server

1、go-zero整合v9 且兼容go-zero redis

这里面简单介绍一下2个框架如何整合redis的

1.1 config

这里simple-admin 框架整合了redis官方v9版本
且github地址:https://github.com/suyuan32/simple-admin-common

go-zero的使用的是自己整合的redis
github.com/zeromicro/go-zero/core/stores/cache

package config

import (
	"github.com/suyuan32/simple-admin-common/config"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/core/stores/cache"
)

type Config struct {
   
   
	rest.RestConf
	// simple-admin
	RedisConf config.RedisConf
	// go-zero
	Cache     cache.CacheConf
}

1.2 svc

svc/service_context.go (根据生成习惯也能是:servicecontext.go)

package svc

import (
	"github.com/zeromicro/go-zero/core/stores/redis"
	"redis1 "github.com/redis/go-redis/v9"
)

type ServiceContext struct {
   
   
	Redis           redis1.UniversalClient
	Config 					config.Config
	RedisCache               *redis.Redis
}

func NewServiceContext(c config.Config) *ServiceContext {
   
   
	// 初始化 cache,其中 WithLimit 可以指定最大缓存的数量
	cCache, err := collection.NewCache(time.Minute, collection.WithLimit(10000))
	if err != nil {
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值